2

我正在尝试使用 curses 模块制作菜单系统。我有以下代码:

import sys, os, traceback, curses

def main(scrn):
    screen = scrn
    screen.border(0)
    curses.start_color()
    curses.init_pair(2,curses.COLOR_WHITE,curses.COLOR_BLUE)

    while True:
        event = screen.getch()
        if event == ord("q"):
            break
        elif event == curses.KEY_RIGHT:
            #enter function containing while loop, passing job
            job_sub()


#loop to hand the subscreen for a job element
def job_sub():

    screen = curses.newwin(5, 10, 3, 3)
    screen.box()
    objects =["RUN", "MAINTAIN", "EDIT"]

    for i in range( len(objects) ):
        if i == 0:
            screen.addstr(i+1,1, objects[i], curses.color_pair(2))
        else:
            screen.addstr(i+1,1, objects[i])

    screen.refresh()

    while True:
        event = screen.getch()
        if event == curses.KEY_LEFT:
            break

    screen.erase()
    return

if __name__=='__main__':
    try:
        # Initialize curses
        screen=curses.initscr()
        curses.noecho()
        curses.cbreak()
        screen.keypad(1)

        main(screen) 

        screen.keypad(0)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
    except:
        # In event of error, restore terminal to sane state.
        screen.keypad(0)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
        traceback.print_exc()

程序一直运行,直到我按下右箭头键。在那之后,它冻结了,就像它被困在一个循环中一样。它不会再响应任何输入。任何帮助表示赞赏。

4

1 回答 1

4

在您的job_sub()功能中,您创建了一个新窗口,但您没有为它启用键盘。结果,箭头键没有发送curses.KEY_LEFT值。

于 2013-06-28T16:34:47.077 回答