与Python诅咒回调(Callbacks with Python curses)

2019-09-17 14:44发布

我有一些代码,看起来像这样:

stdscr.nodelay(1)
while True:
    c = stdscr.getch()
    if c != -1:
        stdscr.addstr("%s was pressed\n" % c)
    if time() - last > 1:
        stdscr.addstr("time() == %s\n" % str(time()))
        last = time()

不过,我担心我的代码确实是浪费/低效。 是否有可用的诅咒回调机制? 如果没有,什么是处理这种情况的典型方式是什么?

Answer 1:

你可以尝试Urwid代替。

Urwid提供诅咒的顶部上的更高层级工具包,并且包括一个事件循环处理键盘和鼠标输入。 它或者使用它自己的选择,基于事件的循环,或可以挂接到GEVENT或扭曲。

除了处理键盘输入有效的你也有一台主机的选项来处理用户输入与编辑框,列表控件等。



Answer 2:

如何使用半延迟模式,使残培()阻塞?

import time
import curses

stdscr = curses.initscr()
curses.halfdelay(10) # 10/10 = 1[s] inteval

try:
    while True:
        c = stdscr.getch()
        if c != -1:
            stdscr.addstr("%s was pressed\n" % c)
        stdscr.addstr("time() == %s\n" % time.time())
finally:
    curses.endwin()


文章来源: Callbacks with Python curses