cornradio/dumb_menu

I don't understand how to use the menu.

Closed this issue ยท 4 comments

I recently started learning Python, and in my first mini-applications I wanted to use a module that draws the menu, instead of the usual "input-method" like

a = input('Choose a number [1/2]: ')
if a == 1:
	print('You have chosen 1!')
elif a == 2:
	print('You have chosen 2!')
else:
	print('Error, try again.')

I don't like the fact that instead of pressing a key, I have to type it in the line to confirm it with Enter.
Your module solves this problem, but I couldn't find the documentation for it, and I have no idea how to use it.
Can I ask you for a use case that could replace my code above?

P.S: I have problems with English, so I use Deepl for translation. I apologize in advance for any strange/inaccurate wording. Thanks in advance for the answer!

in other languages like C , u have things like getchar , but python don't.

windows

i use msvcrt to replace getchar .
msvcrt returns byte , in python you write b'xxx' to comapare those .

it's simple to get keys like 1234\abcd but it's hard to get up down and Function keys ,

mac or linux

i use getch to get keys , this is based on termios , dont support windows .

core code here.

def get_key(): #get keypress using getch , msvcrt = windows or termios = linux
    try :
        import getch
        first_char = getch.getch()
        if first_char == '\x1b': #arrow keys
            a=getch.getch()
            b=getch.getch()
            return {'[A': 'up', '[B': 'down', '[C': 'right', '[D': 'left' }[a+b]
        if ord(first_char) == 10:
            return  'enter'
        if ord(first_char) == 32:
            return  'space'
        else:
            return first_char #normal keys like abcd 1234
    except :
        pass
    try:
        import msvcrt
        key = msvcrt.getch()  # get keypress
        if key == b'\x1b':  # Esc key to exit
            return 'esc'
        elif key == b'\r':  # Enter key to select
            return 'enter'
        elif key == b'\x48':  # Up or Down arrow
            return  'up'
        elif key == b'\x50':  # Up or Down arrow
            return 'down'
        else:
            return key.decode('utf-8')
    except:
        pass

while True:
    key = get_key()
    print("You pressed: ", key)

i have a blog here , if you interest , but this blog is not focus on your problem .
blog

Hi, it's me again! I figured out the module device and I still have a questions.

Can I hide the message Menu current option: 0 and Use the arrow keys to move, Enter/Hotkey to select.?

no it's hard coded.
Maybe in next version:)

Hi, it's me again! I figured out the module device and I still have a questions.

Can I hide the message Menu current option: 0 and Use the arrow keys to move, Enter/Hotkey to select.?

dumb_menu-1.0.4 is uploaded , now you can use :

options = ["[1]Option 1", "[2]Option 2", "[3]Option 3","[q]quit"]
index = get_menu_choice(options,isclean=True)

to decide to use hint or not. ( it's ON by default)