Skip to content

Instantly share code, notes, and snippets.

@cibervicho
Last active February 17, 2024 04:16
Show Gist options
  • Save cibervicho/e3dd442151b1c155d30ac51c29345727 to your computer and use it in GitHub Desktop.
Save cibervicho/e3dd442151b1c155d30ac51c29345727 to your computer and use it in GitHub Desktop.
Reto 5, mejorado
import curses
from curses import wrapper
MENU = [
"Listar usuarios",
"Agregar usuario",
"Ver informacion de usuarios",
"Editar usuarios",
"Eliminar usuarios",
"Salir del programa",
]
def paint_status(window, message, attr):
""" Paints the status bar """
pass
def initializer(stdscr):
""" Initializes the main graphical configuration """
# Turn off cursor blinking
curses.curs_set(0)
# Color scheme for selected row
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
def print_menu(stdscr, selected_row):
""" Prints the menu in the center of the screen """
# Clear the screen
stdscr.clear()
# Getting the screen measurements
h, w = stdscr.getmaxyx()
for idx, opt in enumerate(MENU):
# Calculating the position of the menu
x = w//2 - len(opt)//2
y = h//2 - len(MENU)//2 + idx
if idx == selected_row:
# Highlighting the selected row
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, opt)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, opt)
stdscr.refresh()
def print_center(stdscr, text):
""" Prints a text in the center of the screen """
stdscr.clear()
# Getting the screen measurements
h, w = stdscr.getmaxyx()
# Calculating the position of the text
x = w//2 - len(text)//2
y = h//2
stdscr.addstr(y, x, text)
stdscr.refresh()
def main(stdscr):
# Initializing the graphical configuration
initializer(stdscr)
# Specify the current selected/default row
current_row = 0
# Main loop
while True:
# Print the menu
print_menu(stdscr, current_row)
# Obtain a key from user
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(MENU) - 1:
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
print_center(stdscr, "'{}'".format(MENU[current_row].upper()))
stdscr.getch()
# If user chose last option, exit the program
if current_row == len(MENU) - 1:
break
wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment