【发布时间】:2022-02-07 19:52:38
【问题描述】:
我正在命令窗口(Windows 7、Python 3.1)中运行一个进程,我希望用户通过按 Esc 键中止该进程。但是,按 Esc 似乎没有做任何事情,循环永远不会中断。我也尝试在我的 IDE (Wing) 中运行脚本,但同样,循环不能被中断。
以下是我的概念验证测试的精简版...
import msvcrt
import time
aborted = False
for time_remaining in range(10,0,-1):
# First of all, check if ESCape was pressed
if msvcrt.kbhit() and msvcrt.getch()==chr(27):
aborted = True
break
print(str(time_remaining)) # so I can see loop is working
time.sleep(1) # delay for 1 second
#endfor timing loop
if aborted:
print("Program was aborted")
else:
print("Program was not aborted")
time.sleep(5) # to see result in command window before it disappears!
如果有人能告诉我哪里出错了,我将不胜感激。
【问题讨论】:
标签: python windows escaping keypress