7

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

minhperry
  • 119
  • 1
  • 2
  • 7

3 Answers3

16

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

wong2
  • 34,358
  • 48
  • 134
  • 179
  • 1
    Your module is excellent! It would be fantastic if we could highlight a specific line (I see a package termcolor can highlight the line) :) – Hieu Dang Jul 12 '22 at 16:35
  • Hi, you can open an issue in the Github repo! And PR are always welcomed :) – wong2 Jul 14 '22 at 12:54
  • Very nice solution the only issue with it is that it kind of clears the console when choosing. – Maxime Deuse Aug 18 '22 at 21:45
  • 1
    This is an amazing solution! I was looking for a way to make terminal applications for the Clockwork GameShell, this is the best lead so far for making easy menu selections for the end user. Thanks, @wong2! –  Dec 03 '22 at 02:00
6

You have to detect the keyboard key. As this detect key press in python? answer mentioned, Python has a keyboard module for it.

You can install it with these command

pip install keyboard

Here's how it works

  • Define a menu number range, in this case is 1 until 4
  • Set a default selected menu and represent it with a number that we have defined, so it will appear when the user open the menu, in this case is 1
  • If a user press Up key, you have decrement the selected menu number, except if it has been on the first element of the range. And vice versa for the Down key, you have increment the selected menu number, except if it has been on the last element of the range.
import keyboard

selected = 1

def show_menu():
    global selected
    print("\n" * 30)
    print("Choose an option:")
    for i in range(1, 5):
        print("{1} {0}. Do something {0} {2}".format(i, ">" if selected == i else " ", "<" if selected == i else " "))

def up():
    global selected
    if selected == 1:
        return
    selected -= 1
    show_menu()

def down():
    global selected
    if selected == 4:
        return
    selected += 1
    show_menu()

show_menu()
keyboard.add_hotkey('up', up)
keyboard.add_hotkey('down', down)
keyboard.wait()

Sample

Andra
  • 1,282
  • 2
  • 11
  • 34
  • Unfortunately the current implementation requires root permissions (the keyboard module raises an Error if os.geteuid() != 0). At Import it prints: 'ImportError: You must be root to use this library on linux.' – Hoov Mar 10 '23 at 18:37
6

the above process makes code messier and requires root privileges to run on Linux. the best way is to use enquiries

pip3 install enquiries

and use the following code

import enquiries

options = ['Do Something 1', 'Do Something 2', 'Do Something 3']
choice = enquiries.choose('Choose one of these options: ', options)

print(choice)