Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Online post: https://pastebin.com/zv9EXdgH
- # This is a port of my C# Menu to Python: https://pastebin.com/9NmGvV4y
- # SOURCE: https://docs.python.org/2/library/curses.html
- # SOURCE: https://docs.python.org/3/howto/curses.html
- # For Windows: pip install windows-curses
- import curses
- window = curses.initscr() # Initialize the library. Returns a WindowObject which represents the whole screen.
- window.keypad(True) # Escape sequences generated by some keys (keypad, function keys) will be interpreted by curses.
- curses.cbreak() # Keys are read one by one. Also safer than curses.raw() because you can interrupt a running script with SIGINT (Ctrl + C).
- curses.noecho() # Prevent getch() keys from being visible when pressed. Echoing of input characters is turned off.
- # Initialize colors.
- curses.start_color() # Must be called if you want to use colors.
- curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
- curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
- curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
- curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK)
- curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK)
- curses.init_pair(6, curses.COLOR_BLUE, curses.COLOR_BLACK)
- curses.init_pair(7, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
- curses.init_pair(8, curses.COLOR_CYAN, curses.COLOR_BLACK)
- black = curses.color_pair(1)
- white = curses.color_pair(2)
- red = curses.color_pair(3)
- green = curses.color_pair(4)
- yellow = curses.color_pair(5)
- blue = curses.color_pair(6)
- magenta = curses.color_pair(7)
- cyan = curses.color_pair(8)
- # -----
- def draw_menu(menuItems, selectedIndex):
- # window.erase()
- window.clear()
- # Print a vertical menu.
- for i in range(len(menuItems)):
- window.addstr(' ')
- window.addstr(menuItems[i], black if i == selectedIndex else white)
- window.addstr('\n\n')
- # Print a dividing line.
- window.addstr(('-' * 80) + '\n')
- # Print a horizontal menu.
- for i in range(len(menuItems)):
- window.addstr(' ')
- window.addstr(menuItems[i], black if i == selectedIndex else white)
- window.addstr('\n')
- # -----
- def process_input(menuItems, selectedIndex):
- userInput = window.getch()
- if userInput == curses.KEY_LEFT or userInput == curses.KEY_UP:
- # Loop around backwards.
- selectedIndex = (selectedIndex - 1 + len(menuItems)) % len(menuItems)
- elif userInput == curses.KEY_RIGHT or userInput == curses.KEY_DOWN:
- # Loop around forwards.
- selectedIndex = (selectedIndex + 1) % len(menuItems)
- # If curses.nonl() is called, Enter key = \r else \n.
- elif userInput == curses.KEY_ENTER or chr(userInput) in '\r\n':
- # If the last option, exit, is selected.
- if selectedIndex == len(menuItems) - 1:
- wait_for_any_keypress()
- curses.endwin() # De-initialize the library, and return terminal to normal status. <-- Works without this on Windows, however in Linux you can't type in the terminal after exiting without this :P
- exit(0)
- window.addstr('\n Selected index: {}\n'.format(selectedIndex))
- wait_for_any_keypress()
- else:
- window.addstr("\n The pressed key '{}' {} is not associated with a menu function.\n".format(chr(userInput), userInput))
- wait_for_any_keypress()
- return selectedIndex
- # -----
- def wait_for_any_keypress():
- window.addstr('\n Press any key to continue . . . ')
- window.getch()
- # -----
- def main():
- selectedIndex = 0
- while True:
- draw_menu(MENU_ITEMS, selectedIndex)
- selectedIndex = process_input(MENU_ITEMS, selectedIndex)
- MENU_ITEMS = [
- ' Option 1 ',
- ' Option 2 ',
- ' Option 3 ',
- ' Exit ',
- ]
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement