一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2016-02-29 编辑:简简单单 来源:一聚教程网

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')

热门栏目