我最近在我的机器上安装了windows-curses
模块,通过在一个提升的命令提示符下运行py -m pip install windows-curses
。
test
打印到控制台。然而,什么也没发生——即终端保持空白,不显示任何字符。
#initialise curses
import curses
stdscr=curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
#write text
stdscr.addstr(0,0,'test')
#loop program so the terminal does not close
while 1:
pass
我试过把addstr
调用在while循环内,以及使用wrapper
,但都没有解决这个问题。发生了什么,我该如何解决这个问题?提前感谢!
你必须刷新屏幕。当你请求输入时,Curses会进行物理屏幕更新,例如使用window.getch()
或直接调用window.refresh()
。试试下面的代码片段:
#write text
stdscr.addstr(0, 0, 'test')
#loop program so the terminal does not close
while 1:
c = stdscr.getch()
key = curses.keyname(c)
if key == b'q':
break
else:
stdscr.addstr(1, 0, repr(key))
curses.endwin()