python curses - .getch()在newwin中不起作用

时间:2013-06-26 18:46:17

标签: python curses getch

我正在尝试使用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()

程序一直运行,直到我按下右箭头键。在那之后,它会冻结,就像它被卡在一个循环中一样。它不会对任何更多的输入作出回应。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

job_sub()功能中,您可以创建一个新窗口,但不要为其启用键盘。因此,箭头键不会发送curses.KEY_LEFT值。