How do I create an interactive selection?

大憨熊 提交于 2019-12-10 17:09:03

问题


I want to create an "interactive selection" in Python (not sure if I worded that correctly, look at the example below) that lets users press their up and down keys to choose an option and return to confirm it, then it continues with the script. An example output in a terminal would be something like this:

$ python script.py
Please select an option.

  [ ] Option 1
  [ ] Option 2
  [*] Option 3

You have chosen: Option 3

After that first string had printed, the user could use their arrow keys to choose their option marked by the asterisk. The script would look somewhat like this:

print 'Please select an option.'
option = interactive_choice(['Option 1', 'Option 2', 'Option 3'])
print 'You have chosen: ' + option

Anyone know how to do this?


回答1:


You are very limited with these kind of things on Windows. Libraries you may want to look in to are wcurses, PDCurses and Wconsio. Please note I used neither of these libraries myself. You also might be better off designing a GUI with a IDE like Boa Constructor that looks like a terminal, but no doubt that's considerably more difficult.



来源:https://stackoverflow.com/questions/12132333/how-do-i-create-an-interactive-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!