【问题标题】:Getch gets input before it is calledGetch 在调用之前获取输入
【发布时间】:2017-03-27 19:56:22
【问题描述】:

我正在用 python 编写一个 curses 应用程序。

def main(stdscr):
    stuff(stdscr)

def printStuff(stdscr, string):
    stdscr.clear()
    stdscr.addstr(0, 0, string)
    stdscr.refresh()

def stuff(stdscr):
    printStuff(stdscr, 'foo')
    time.sleep(3)
    printStuff(stdscr, 'bar')
    stdscr.getch()

wrapper(main)

当我运行代码时,大部分情况下一切都很好。 如果我在睡眠时的三秒间隔内按下按钮,getch 会很乐意将其作为输入,即使在调用 getch 之前按下按钮也是如此。为什么会这样,我该如何解决?

【问题讨论】:

  • 你所做的一切都被放入一个输入队列。 getch 处理队列中的任何内容。

标签: python python-curses


【解决方案1】:

输入被缓冲,getch() 处理输入缓冲区中的任何内容。您可以使用curses.flushinp 清除缓冲区

def stuff(stdscr):
    printStuff(stdscr, 'foo')
    time.sleep(3)
    printStuff(stdscr, 'bar')
    stdscr.flushinp()
    stdscr.getch()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2017-04-18
    • 2018-06-20
    相关资源
    最近更新 更多