Chinaunix
标题:
请教如何设置raw_input的等待时间?
[打印本页]
作者:
风中有只鸟
时间:
2012-04-04 07:37
标题:
请教如何设置raw_input的等待时间?
例如设置多少秒没有输入就自动跳过
作者:
anonymous0502
时间:
2012-04-04 09:40
本帖最后由 anonymous0502 于 2012-04-04 09:40 编辑
这有个别人写的例子,我试了能运行:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import msvcrt
def readInput(caption, default, timeout=5):
start_time = time.time()
sys.stdout.write('%s(%s):' % (caption, default))
input = ''
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13: # enter_key
break
elif ord(chr) >= 32:
# space_char
input += chr
if len(input) == 0 and time.time() - start_time > timeout:
break
print '' # needed to move to next line
if len(input) > 0:
return input
else:
return default
# and some examples of usage
ans = readInput('Please type a name', 'john')
print 'The name is %s' % ans
ans = readInput('Please enter a number', 10)
print 'The number is %s' % ans
复制代码
作者:
风中有只鸟
时间:
2012-04-04 12:09
上面的代码测试成功,不过不太明白代码的含义
尝试改为easygui.enterbox("Enter Code: "),却不成功
作者:
anonymous0502
时间:
2012-04-04 13:08
没有用过easygui,不太清楚了。
作者:
风中有只鸟
时间:
2012-04-04 20:24
多谢,继续折腾看看
作者:
5iwww
时间:
2012-04-10 06:50
if len(input) == 0 and time.time() - start_time > timeout:
break
关键就是这一句
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2