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

更新时间:2020-08-24    来源:python    手机版     字体:

【www.bbyears.com--python】

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

本文来源:http://www.bbyears.com/jiaocheng/94497.html

热门标签

更多>>

本类排行