Getch在调用之前获取输入

2024-05-29 03:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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)

当我运行代码时,大部分都是好的。 如果我在睡眠时按一个3秒间隔的按钮,getch会很高兴地将其作为输入,即使在调用getch之前按下了该按钮。为什么会这样,我怎样才能解决这个问题?你知道吗


Tags: 应用程序stringfootimemaindef按钮refresh
1条回答
网友
1楼 · 发布于 2024-05-29 03:48:28

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

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

相关问题 更多 >

    热门问题