在超时的情况下查询用户的输入。

pytimedinput的Python项目详细描述


说明

标准Python input()-函数的一个小而简单的替代方法,允许您指定函数的超时。在

pytimedinput应该可以在Windows和Linux上运行,尽管还没有进行非常广泛的测试,而且可能会有bug。在

安装

$ pip3 install pytimedinput

使用

timedInput()

timedInput()的工作原理类似于Python的默认input()-函数,要求用户提供一个文本字符串,但是timedInput()允许您定义用户输入任何文本的时间量,或者在用户空闲时等待输入的连续秒数。按ENTER键、回车键或ESC键将结束输入并从功能返回。在

^{pr2}$

来自pytimedinput的函数timedInput()接受以下参数:

  • prompt, str: a string to show the user as a prompt when waiting for input.
    Defaults to an empty string.
  • timeout, int: how many seconds to wait before timing out.
    Defaults to 5 seconds.
  • forcedTimeout, bool: whether to wait for ‘timeout’ many consecutive seconds of idle time or simply time out regardless of user-input.
    Defaults to False, ie. consecutive.
  • maxLength [], int: the maximum length of the string user is allowed to enter.
    Defaults to 0, ie. unlimited.

函数返回一个元组:

  • str: a string containing whatever the user typed, regardless of whether the function timed out or not.
  • bool: whether the function timed out or not.
frompytimedinputimporttimedInputuserText,timedOut=timedInput("Please, do enter something: ")if(timedOut):print("Timed out when waiting for input.")print(f"User-input so far: '{userText}'")else:print(f"User-input: '{userText}'")

timedKey()

timedKey()等待用户按一组预定义键中的一个,并超时,同时忽略列表中没有的任何键。在

deftimedKey(prompt="",timeOut=5,forcedTimeout=False,allowCharacters=['y','n'])

来自pytimedinput的函数timedKey()接受以下参数:

  • prompt, str: a string to show the user as a prompt when waiting for input.
    Defaults to an empty string.
  • timeout, int: how many seconds to wait before timing out.
    Defaults to 5 seconds.
  • forcedTimeout, bool: whether to wait for ‘timeout’ many consecutive seconds of idle time or simply time out regardless of user-input.
    Defaults to False, ie. consecutive.
  • allowCharacters [], list: list of valid characters for the user to use.
    Defaults to ‘y’ and ‘n’.

函数返回一个元组:

  • str: a string containing the key user pressed, if on the endCharacters - list, or an empty string.
  • bool: whether the function timed out or not.
frompytimedinputimporttimedKeyuserText,timedOut=timedKey("Please, press 'y' to accept or 'n' to decline: ",endCharacters=['y','n'])if(timedOut):print("Timed out when waiting for input. Pester the user later.")else:if(userText=="y"):print("User consented to selling their first-born child!")else:print("User unfortunately declined to sell their first-born child!")

timedInteger()和timedFloat()

timedInteger()timedFloat的工作方式与timedInput()类似,只是在timedFloat的情况下,只允许用户输入数字和逗号或句点。在

deftimedInteger(prompt="",timeOut=5,forcedTimeout=False,maxLength=0)

来自pytimedinput的函数timedInteger()timedFloat()接受以下参数:

  • prompt, str: a string to show the user as a prompt when waiting for input.
    Defaults to an empty string.
  • timeout, int: how many seconds to wait before timing out.
    Defaults to 5 seconds.
  • forcedTimeout, bool: whether to wait for ‘timeout’ many consecutive seconds of idle time or simply time out regardless of user-input.
    Defaults to False, ie. consecutive.
  • maxLength [], int: the maximum length of numbers the user is allowed to enter. The decimal-separator does not count.
    Defaults to 0, ie. unlimited.

函数返回一个元组:

  • int/float or None: an integer or float, depending on which function was called or None, if no number was entered.
  • bool: whether the function timed out or not.
frompytimedinputimport*userText,timedOut=timedInput("Please, do enter something: ")if(timedOut):print("Timed out when waiting for input.")print(f"User-input so far: '{userText}'")else:print(f"User-input: '{userText}'")userNumber,timedOut=timedFloat("Enter a number: ",maxLength=5)if(nottimedOut):if(userNumber==None):print("We wanted a number, but got none.")else:print(f"We should do some fancy maths with {userNumber}!")

例外情况

timedInput()timedKey()都需要一个交互式shell来运行,否则将引发一个Runtimerror-异常,这将需要捕捉到将以交互方式和非交互方式使用的任何脚本中。在

许可证

麻省理工学院

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在jetty请求日志中添加milliscond字段?   java在使用Scanner类从文件读取信息时遇到问题   为什么。类不适用于泛型类型?   SQLite插入上的java空指针异常   java能告诉我们Guava缓存是在禁用统计数据的情况下构建的吗?   java在应用程序中使用常量   java无法使用AutoIT和Selenium Webdriver在所需位置/文件夹保存图像   java如何在jtable中更新jprogress栏   java是比较给定日期和当前日期(在给定时区中没有时间段)的最佳方法   安卓代码中的java错误   java无法访问实体类中的字段   java如何在tomcat中处理三个JDBC连接池?   java无法使用Spring Security保护AngularJS页面   如何在没有TCP/IP协议栈的情况下用Java发送以太帧