【发布时间】:2017-06-11 04:24:19
【问题描述】:
在我在 Windows 7 Professional 64 上运行的以下程序中,我试图允许用户在需要时进行干预(通过内部 while 循环)并导致外部 while 循环重复操作.否则,内部 while 循环将超时,程序将继续畅通无阻:
import msvcrt
import time
decision = 'do not repeat' # default setting
for f in ['f1', 'f2', 'f3']:
print ('doing some prepartory actions on f')
while True: # outer while loop to allow repeating actions on f
print ('doing some more actions on f')
t0 = time.time()
while time.time() - t0 < 10: # inner while loop to allow user to intervene
if msvcrt.kbhit(): # and repeat actions by pressing ENTER if
if msvcrt.getch() == '\r': # needed or allow timeout continuation
decision = "repeat"
break
else:
break
time.sleep(0.1)
if decision == "repeat":
print ("Repeating f in the outer while loop...")
continue
else:
break
print ('doing final actions on f in the for loop')
但是,内部循环的用户输入部分(按 ENTER 重复)不起作用,我不知道为什么。我从here 提供的解决方案中获得了它的想法。 关于如何让它发挥作用的任何想法?
【问题讨论】:
-
kbhit和getch要求将进程附加到控制台窗口。如果您使用的是 IDLE,则该进程没有控制台——至少在使用 pythonw.exe 以默认方式运行时没有。即使您确实使用附加的控制台运行 IDLE(例如,使用 Win+R 运行对话框来运行py -3 -m idlelib),我怀疑您是否希望用户必须切换到控制台窗口才能输入输入。 -
反正IDLE等IDE shell只是开发环境。如果您打算将其用作控制台脚本,则可以模拟假控制台 I/O 函数,以便在没有附加控制台时用于测试(例如,
open("CONIN$")失败)。如果它不应该是控制台程序,那么使用 GUI 工具包创建您自己的窗口并读取键盘输入。
标签: python windows loops timeout controls