Python 3.4.3等待用户输入超时自动跳过

来源:赵克立博客 分类: Python 标签:Python发布时间:2016-02-16 00:36:35最后更新:2016-12-09 14:32:47浏览:7671
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2016-12-09 14:32:47
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
import sys,time,msvcrt
def readInput(caption, default, timeout=10):
    start_time = time.time()
    sys.stdout.write('%s(%d秒自动跳过):' % (caption,timeout))
    sys.stdout.flush()
    input = ''
    while True:
        ini=msvcrt.kbhit()
        try:
            if ini:
                chr = msvcrt.getche()
                if ord(chr) == 13:  # enter_key
                    break
                elif ord(chr) >= 32:
                    input += chr.decode()
        except Exception as e:
            pass
        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
 
 
#使用方法
iscaiji=readInput('请输入你的名字','aaa')



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python