Python curses菜单不起作用

时间:2015-12-23 19:21:37

标签: python python-2.7 curses

尝试使用一个非常基本的curses菜单。我一直在关注一个教程,但我有点分手做自己的事情。我尝试用评论来触及所有内容。我只想弄清楚如何让选择工作。

import sys
import curses
import time

def menu(window, dims):
    window.nodelay(0)                      # not sure what this does, lol
    window.clear()                         # clear window
    selection = -1                         # the menu code is from the tutorial
    option = 0                             # so most of this is foreign to me
    while selection < 0:                   # loop
        graphics = [0]*2                   # point to list
        graphics[option] = curses.A_REVERSE # set graphic, reverse BKGND color
        window.border()                    # border
        window.addstr(1, dims[1]/2-2, "Menu")
        window.addstr(3, dims[1]/2-2, "Play", graphics[0])
        window.addstr(4, dims[1]/2-2, "Exit", graphics[1])
        window.refresh()                   # refresh the window
        action = window.getch()            # get input
        if action == curses.KEY_UP:        # if key is up
            option = (option - 1)          # go up?
        elif action == curses.KEY_DOWN:    # key is down
            option = (option + 1)          # go down?
        elif action == ord('\n'):          # if RETURN
            selection = option             # make selection
    window.clear()                         # clear all
    if selection == 0:                     # if is 0, do nothing right now.
        return                             # it's not changing selection
    elif selection == 1:                   # so waiting for that to add here
        return

def main():                                # main function // ran first
    window = curses.initscr()              # making new window
    dims = window.getmaxyx()               # getting window dimensions
    if not curses.has_colors():            # color test
        curses.endwin()                    # color test
        print "no colors"                  # color test
        sys.exit()                         # color test
    else:                                  # color test
        curses.start_color()               # color test
    curses.noecho()                        # don't echo the keys on the screen
    curses.cbreak()                        # don't wait enter for input
    curses.curs_set(0)                     # don't show cursor
    main_loop(window, dims)                # go into main loop

def main_loop(window, dims):
    while menu(window, dims):
        pass
    window.clear()                         
    window.addstr(dims[0]/2, (dims[1]-4)/2, "Exiting...")
    window.refresh()
    time.sleep(1)
    curses.noecho()
    curses.cbreak()
    curses.curs_set(1)
    curses.endwin()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

我错过了window.keypad(1)来实际激活输入 我在main_loop()之前添加了它。