我想使用curses
在终端中动态打印文本,但是当按下这种键时,以下代码不会显示键向上/向下文本,而循环文本会更新。我做错了什么?我是Mac OS Maverick。
import curses
import time
screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
screen.keypad(1)
screen.nodelay(1)
i = 0
while True:
i += 1
event = screen.getch()
screen.clear()
if event == ord("q"):
break
elif event == curses.KEY_UP:
screen.addstr("The User Pressed UP", curses.A_REVERSE)
time.sleep(3)
elif event == curses.KEY_DOWN:
screen.addstr("The User Pressed DOWN", curses.A_BLINK)
time.sleep(3)
else:
screen.addstr("Loop {0}".format(i))
curses.endwin()
我已经测试了上面的第二个代码并且效果很好。对我来说很奇怪......
import curses
screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
screen.keypad(1)
screen.nodelay(1)
i = 0
while True:
i += 1
event = screen.getch()
if event == ord("q"):
break
elif event == curses.KEY_UP:
screen.clear()
screen.addstr(
"The User Pressed UP in the loop {0}".format(i),
curses.A_REVERSE
)
elif event == curses.KEY_DOWN:
screen.clear()
screen.addstr(
"The User Pressed DOWN in the loop {0}".format(i),
curses.A_BLINK
)
curses.endwin()
答案 0 :(得分:0)
解决方案是使用screen.refresh()
。
import curses
import time
screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
screen.keypad(1)
screen.nodelay(1)
i = 0
while True:
i += 1
event = screen.getch()
screen.clear()
if event == ord("q"):
break
elif event == curses.KEY_UP:
screen.addstr("The User Pressed UP", curses.A_REVERSE)
screen.refresh()
time.sleep(2)
elif event == curses.KEY_DOWN:
screen.addstr("The User Pressed DOWN", curses.A_BLINK)
screen.refresh()
time.sleep(2)
else:
screen.addstr("Loop {0}".format(i))
curses.endwin()