我是python的n00b,我正在寻找执行以下操作的代码片段/示例:
谢谢您的帮助!
Yvan Janssens
Bry*_*yan 18
如果你在Unix/Linux上,那么select模块将帮助你.
import sys
from select import select
print "Press any key to configure or wait 5 seconds..."
timeout = 5
rlist, wlist, xlist = select([sys.stdin], [], [], timeout)
if rlist:
print "Config selected..."
else:
print "Timed out..."
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Windows,请查看msvcrt模块.(注意这在IDLE中不起作用,但在cmd提示符下会有效)
import sys, time, msvcrt
timeout = 5
startTime = time.time()
inp = None
print "Press any key to configure or wait 5 seconds... "
while True:
if msvcrt.kbhit():
inp = msvcrt.getch()
break
elif time.time() - startTime > timeout:
break
if inp:
print "Config selected..."
else:
print "Timed out..."
Run Code Online (Sandbox Code Playgroud)
编辑更改了代码示例,以便您可以判断是否存在超时或按键...
| 归档时间: |
|
| 查看次数: |
9253 次 |
| 最近记录: |