Python:如何使用列表作为用户输入的选择源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28425204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: how to use list as source of selection for user input?
提问by Srini
Can anyone check this code and let me know what is wrong?
任何人都可以检查此代码并让我知道有什么问题吗?
input_list = ["One", "Two", "Three"]
P1 = input("Select the input: ", input_list[0], input_list[1], input_list[2])
print (P1)
回答by badAPI
Take a look at the documentation of the input
function: https://docs.python.org/2/library/functions.html#input
看看input
函数的文档:https: //docs.python.org/2/library/functions.html#input
input
presents a prompt and evaluates the data input by the user as if it were a Python expression. If you just want to gather data input by the user, use raw_input
instead. You'll need to implement custom logic to make sure the user's input matches with something in your list.
input
呈现提示并评估用户输入的数据,就像它是 Python 表达式一样。如果您只想收集用户输入的数据,请raw_input
改用。您需要实现自定义逻辑以确保用户的输入与列表中的内容相匹配。
回答by Ehvince
With python's raw_input
it is not possible to give a preselected list to the user to choose from. With raw_input
we collect raw strings.
使用 pythonraw_input
无法为用户提供预选列表以供选择。随着raw_input
我们收集原始字符串。
update: a nice solution is to use the new pick library: https://github.com/wong2/pickIt provides a small curses interface to pick our choice from a given list. Get it with pip install pick
. (update: multi-select: yes)
更新:一个不错的解决方案是使用新的选择库:https: //github.com/wong2/pick它提供了一个小的 curses 界面来从给定的列表中选择我们的选择。得到它pip install pick
。(更新:多选:是)
update 2:?and yet another python lib ! https://curses-menu.readthedocs.org/en/latest/usage.html#getting-a-selection(no multi-select)
更新 2:? 还有另一个 python 库!https://curses-menu.readthedocs.org/en/latest/usage.html#getting-a-selection(无多选)
There's a tiny and unmaintained library made for that purpose, picker(multi-select: yes).
有一个为此目的而制作的小型且未维护的库,picker(多选:是)。
The simplest solution I'm thinking of is to use shell tools:
我想到的最简单的解决方案是使用 shell 工具:
dialog
is what distros like Debian use to present UIs in the console,- selectais a fuzzy text selector for the shell, so it fits exactly our needs, except it is a ruby tool,
zenity (and yad-dialog) make it very easy to build simple windows (we exit the terminal). I can display a list with this:
zenity --list --text="a title" --column="first column" "first choice" "second choice"
We can also select multiple choices.
- if we exit the console, we can use more complete GUI tools like gooey (it turns a python script with command line arguments into a GUI) or even Flexxx and others, but that's another work.
dialog
是像 Debian 这样的发行版用来在控制台中呈现 UI 的东西,- selecta是一个用于 shell 的模糊文本选择器,所以它完全符合我们的需求,除了它是一个 ruby 工具,
zenity(和yad-dialog)使构建简单窗口变得非常容易(我们退出终端)。我可以用这个显示一个列表:
zenity --list --text="a title" --column="first column" "first choice" "second choice"
我们也可以选择多个选项。
- 如果我们退出控制台,我们可以使用更完整的 GUI 工具,如 gooey(它将带有命令行参数的 python 脚本转换为 GUI)甚至 Flexxx等,但这是另一项工作。