在python中等待输入1秒
我正试图用Python制作一个使用curses的note应用程序。 在左下角,应该是一个每秒钟更新一次的时钟 我现在遇到的问题是,它要么休眠1秒,要么等待输入 如果没有输入,是否可以等待输入1秒并继续 我之所以要这样做,是为了防止在应用程序中移动时出现延迟 我想多线程之类的东西可以完成这项工作,但也遇到了一些问题 这是我目前掌握的代码:在python中等待输入1秒,python,curses,Python,Curses,我正试图用Python制作一个使用curses的note应用程序。 在左下角,应该是一个每秒钟更新一次的时钟 我现在遇到的问题是,它要么休眠1秒,要么等待输入 如果没有输入,是否可以等待输入1秒并继续 我之所以要这样做,是为了防止在应用程序中移动时出现延迟 我想多线程之类的东西可以完成这项工作,但也遇到了一些问题 这是我目前掌握的代码: #!/usr/bin/env python3 import curses import os import time import datetime impor
#!/usr/bin/env python3
import curses
import os
import time
import datetime
import threading
def updateclock(stdscr):
while True:
height, width = stdscr.getmaxyx()
statusbarstr = datetime.datetime.now().strftime(' %A')[:4] + datetime.datetime.now().strftime(' %Y-%m-%d | %H:%M:%S')
stdscr.addstr(height-1, 0, statusbarstr)
time.sleep(1)
def draw_menu(stdscr):
k = 0
stdscr.clear()
stdscr.refresh()
threading.Thread(target=updateclock, args=stdscr).start()
cursor_y = 0
cursor_x = 0
while (k != ord('q')):
#while True:
stdscr.clear()
height, width = stdscr.getmaxyx()
stdscr.addstr(height//2, width//2, "Some text in the middle")
if k == curses.KEY_DOWN:
cursor_y = cursor_y + 1
elif k == curses.KEY_UP:
cursor_y = cursor_y - 1
elif k == curses.KEY_RIGHT:
cursor_x = cursor_x + 1
elif k == curses.KEY_LEFT:
cursor_x = cursor_x - 1
stdscr.refresh()
#time.sleep(1)
# Wait for next input
k = stdscr.getch()
curses.wrapper(draw_menu)
代码看起来很混乱,这是我第一次主要关注curses函数
是否可以只等待输入
k=stdscr.getch()
1秒?默认情况下,getch将阻止输入,直到您准备好字符输入。如果nodelay mode为True,则您将获得已准备好的字符的字符值(0-255),或者获得-1,表示没有字符值已准备好
stdscr.nodelay(True) #Set nodelay to be True, it won't block anymore
k = stdscr.getch() #Either the next character of input, or -1
令人惊叹的!!首先我试了一下,所有的东西都开始眨眼了。我认为这是因为它不断刷新输出。我将延迟设置为0.05秒,延迟是不可见的。非常感谢。