我正在使用curses库在终端中编写一个应用程序。它工作正常,但我有一个想法,添加一些击键,以及用户可以在屏幕上选择实体并获得信息面板的功能(我还需要弄清楚这一点)。然而,在使用
.getch()
添加关键侦听器时,我的 systeminfo
面板未绘制,我想知道为什么,并理解这是因为 .getch()
被阻塞。我如何防止这种阻塞行为?
def setup_stdscr():
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.curs_set(0)
stdscr.nodelay(True)
_init_colors()
return stdscr
...
if __name__ == "__main__":
stdscr = setup_stdscr()
stdscr.refresh()
win = curses.newwin(0, 0)
win.refresh()
width = win.getmaxyx()[1]
height = win.getmaxyx()[0]
args = _get_args(sys.argv)
sysinfo = None
if args.sysinfo:
sysinfo = SystemInfo(win)
if sysinfo is not None:
width = width
height = height - 2
ov = Overworld(win, width, height)
sys = System(ov, sysinfo)
curses.wrapper(sys.run())
def run(self):
self.overworld.spawn_entities()
while True:
self.overworld.update()
self.overworld.draw()
c = self.overworld.stdscr.getch()
if c:
if c == ord("q"):
break
if c == curses.KEY_MOUSE:
_, mx, my, _, state = curses.getmouse()
print(mx, my, state)
if state & curses.BUTTON1_PRESSED:
entity = self.overworld.get_entity_at(mx, my)
print(entity)
if entity:
self.show_entity_info(self.overworld.stdscr, entity)
if self.system_info:
self.system_info.draw()
self.overworld.stdscr.refresh()
curses.napms(MINUTE_LENGTH * 1000)
bus.emit("minute:passed")
感谢来自 Python Discord 服务器的 @zokona,我解决了这个问题。他们发现了这个https://davesteele.github.io/development/2021/08/29/python-asyncio-curses/