1

为什么当我运行此代码时box.getch()返回错误的值,而当我更改box.getch()screen.getch()它时返回正确的值?我一直在网上寻找,没有人说getch()只适用于屏幕。如果您按下其中一个箭头,它将返回 27,这是 ESC 的字符。(此代码应打印字符,直到用户按 ESC ...)

import curses
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad( 1 )
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
highlightText = curses.color_pair( 1 )
normalText = curses.A_NORMAL
screen.border( 0 )
curses.curs_set( 0 )
box = curses.newwin( 22, 64, 1, 1 )
box.box()
box.addstr( 14, 3, "YOU HAVE PRESSED: ")

screen.refresh()
box.refresh()

x = box.getch()
while x != 27:
    box.erase()
    box.addstr( 14, 3, "YOU HAVE PRESSED: " + str(x) )
    screen.border( 0 )
    box.border( 0 )
    screen.refresh()
    box.refresh()
    x = box.getch()

curses.endwin()
exit()
4

2 回答 2

1
#!/usr/bin/python3
import          curses
screen        = curses.initscr(      )
screen.border ( 0                    )
e             = curses.noecho(       )
e             = curses.cbreak(       )
e             = curses.start_color(  )
e             = curses.init_pair(  1 , curses.COLOR_GREEN, curses.COLOR_CYAN)
e             = curses.curs_set(   0 )
highlightText = curses.color_pair( 1 )
normalText    = curses.A_NORMAL
box           = curses.newwin(  20, 60, 1, 1 )
box.keypad( 1 )
box.box(      )
box.refresh(  )
e =   x  =  1
try:
  while x != 50 :            #     27  ESC
    box.erase(       )
    cc="WTF?"
    try:     cc=str(chr(x))
    except:  cc="ERR"
    box.addstr( 8, 8, "key 2 to quit, you pressed" + str(x) + "  '" + cc + "'" )
    screen.border( 0 )
    box.border(    0 )
    x = box.getch(   )
except : print("oh my!")
finally: curses.endwin() # avoids messed up console!
于 2019-11-16T00:56:21.810 回答
0

答案(请参阅python curses 中的带有刷新的错误)是添加box.keypad(1). 有几行是不必要的 - 这些在示例中被标记:

    导入诅咒
    屏幕 = curses.initscr()
    诅咒.noecho()
    诅咒.cbreak()
    curses.start_color()
    screen.keypad(1) # 删除这一行
    curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
    highlightText = curses.color_pair(1)
    normalText = curses.A_NORMAL
    screen.border(0)
    诅咒.curs_set(0)
    盒子 = curses.newwin(22, 64, 1, 1)
    box.keypad(1)
    盒子.盒子()
    box.addstr(14, 3, "你已经按下:")

    screen.refresh() # 删除这一行
    box.refresh()

    x = box.getch()
    而 x != 27:
        box.erase()
        box.addstr( 14, 3, "你已经按下:" + str(x) )
        screen.border(0)
        box.border(0)
        screen.refresh() # 删除这一行
        box.refresh() # 删除这一行
        x = box.getch()

    诅咒.endwin()
    出口()
于 2015-06-21T00:40:49.913 回答