Hi there folks,
I suddenly realized in the design of a Python script that the best would be to allow my users to make selections using menus. You got the idea: you are given a menu, you select an option and thus, another menu is shown and so forth until a terminal selection is made and the python script performs the associated action. I'm not interested in graphical menus but just plain text/ascii menus.
It impressed me quite a lot that I did not found any specific package for this. Do you guys have any recommendation?
Thanks a lot!
using Python Menu package? - Stack Overflow
python - CLI Menu library - Code Review Stack Exchange
command line interface - Python CLI Menu with Arrow Keys on Windows - Stack Overflow
Creating a Menu in Python - Stack Overflow
Videos
» pip install console-menu
» pip install Menu
» pip install pymenu-console
The cutie library might be what you want. Example:
options = [f'Choice {i}' for i in range(1, 5)]
chosen_idx = cutie.select(options)
chosen = options[chosen_idx]
In Windows OS (and only for 32 bits Python versions), you can use 'unicurses' module. You can download it from pypi.org (https://pypi.org/project/UniCurses/1.2/) and install it as a .exe file.
when you have already installed it, it will appear an 'import module error', all about it is explained on this post here in SOF just in case: UniCurses pdcurses.dll Error
Basically you have to download 'pdcurses.dll' (here is a link: https://www.opendll.com/index.php?file-download=pdcurses.dll&arch=32bit&version=3.4.0.0) and move it to a specific directory. It's detalled on the post above.
Here is an example code of how this module works to use it with Arrow Keys:
from unicurses import *
WIDTH = 30
HEIGHT = 10
startx = 0
starty = 0
choices = ["Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"]
n_choices = len(choices)
highlight = 1
choice = 0
c = 0
def print_menu(menu_win, highlight):
x = 2
y = 2
box(menu_win, 0, 0)
for i in range(0, n_choices):
if (highlight == i + 1):
wattron(menu_win, A_REVERSE)
mvwaddstr(menu_win, y, x, choices[i])
wattroff(menu_win, A_REVERSE)
else:
mvwaddstr(menu_win, y, x, choices[i])
y += 1
wrefresh(menu_win)
stdscr = initscr()
clear()
noecho()
cbreak()
curs_set(0)
startx = int((80 - WIDTH) / 2)
starty = int((24 - HEIGHT) / 2)
menu_win = newwin(HEIGHT, WIDTH, starty, startx)
keypad(menu_win, True)
mvaddstr(0, 0, "Use arrow keys to go up and down, press ENTER to select a choice")
refresh()
print_menu(menu_win, highlight)
while True:
c = wgetch(menu_win)
if c == KEY_UP:
if highlight == 1:
highlight == n_choices
else:
highlight -= 1
elif c == KEY_DOWN:
if highlight == n_choices:
highlight = 1
else:
highlight += 1
elif c == 10: # ENTER is pressed
choice = highlight
mvaddstr(23, 0, str.format("You chose choice {0} with choice string {1}", choice, choices[choice-1]))
clrtoeol()
refresh()
else:
mvaddstr(22, 0, str.format("Character pressed is = {0}", c))
clrtoeol()
refresh()
print_menu(menu_win, highlight)
if choice == 5:
break
refresh()
endwin()
And here is an image of the result on the command line interface (CMD):

def my_add_fn():
print "SUM:%s"%sum(map(int,raw_input("Enter 2 numbers seperated by a space").split()))
def my_quit_fn():
raise SystemExit
def invalid():
print "INVALID CHOICE!"
menu = {"1":("Sum",my_add_fn),
"2":("Quit",my_quit_fn)
}
for key in sorted(menu.keys()):
print key+":" + menu[key][0]
ans = raw_input("Make A Choice")
menu.get(ans,[None,invalid])[1]()
There were just a couple of minor amendments required:
ans=True
while ans:
print ("""
1.Add a Student
2.Delete a Student
3.Look Up Student Record
4.Exit/Quit
""")
ans=raw_input("What would you like to do? ")
if ans=="1":
print("\n Student Added")
elif ans=="2":
print("\n Student Deleted")
elif ans=="3":
print("\n Student Record Found")
elif ans=="4":
print("\n Goodbye")
elif ans !="":
print("\n Not Valid Choice Try again")
I have changed the four quotes to three (this is the number required for multiline quotes), added a closing bracket after "What would you like to do? " and changed input to raw_input.