Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console select menu in python

I have a idea to create a console select menu in python like this:

Choose an option:
> 1. Do something 1 <
  2. Do something 2
  3. Do something 3
  4. Do something 4

If I press up arrow key, nothing happens. If I press the down one, the less than and greater than symbol would move up and down like this:

Choose an option:
  1. Do something 1 
> 2. Do something 2 <
  3. Do something 3
  4. Do something 4

But I dont know which Python 3 module would help me catch the key press instead of input(), and idk how I can align it correctly.

My solution for alignment is to print spaces (maybe?) and when the key press event is catches, the console will be cleared and it prints the select menu again instead of changing/modifying the strings.

Also, the options would be get from a list, which means this menu is expandable

like image 934
minhperry Avatar asked Dec 17 '22 16:12

minhperry


1 Answers

I wrote a Python module pick for this, it has an easy to use api and supports Windows

https://github.com/wong2/pick

from pick import pick

title = 'Please choose your favorite programming language: '
options = ['Java', 'JavaScript', 'Python', 'PHP', 'C++', 'Erlang', 'Haskell']

option, index = pick(options, title, indicator='=>', default_index=2)

enter image description here

like image 106
wong2 Avatar answered Dec 20 '22 06:12

wong2