raw_input和timeout

38
我想要执行一个raw_input('输入一些内容: .')。我希望它暂停3秒钟,如果没有输入,则取消提示并运行其余的代码。然后代码循环并再次执行raw_input。如果用户输入像'q'这样的东西,我也希望它中断。

1
相关:Python 3 定时输入 /15528939 - n611x007
1
相关:Python函数调用超时/492519 - n611x007
1
如何在输入中设置时间限制/2933399 - n611x007
可移植、仅限于标准库、仅限于线程,在此重复的答案被接受 这里 - Darkonaut
4个回答

62

有一个简单的解决方案,不需要使用线程(至少不需要显式地使用线程):使用select来知道何时可以从标准输入读取内容:

import sys
from select import select

timeout = 10
print "Enter something:",
rlist, _, _ = select([sys.stdin], [], [], timeout)
if rlist:
    s = sys.stdin.readline()
    print s
else:
    print "No input. Moving on..."

编辑[0]: 显然这个在Windows上行不通,因为select()的底层实现需要一个套接字,而sys.stdin不是。 感谢@Fookatchu提供信息。


1
select 模块在 Python 2.3 中。 - habnabit
1
我遇到了这个错误:Traceback (most recent call last): File "Z:\test.py", line 6, in <module> rlist, _, _ = select([sys.stdin], [], [], timeout) TypeError: argument must be an int, or have a fileno() method. - ykmizu
5
好的。顺便说一句,在打印提示后,我需要执行sys.stdout.flush()才能看到它。 - mykhal
今天我一直在努力实现带有超时的键盘输入。我只是想要一种停止从硬盘中复制图像的方法,以便我可以通过按下一个键来停止它,因此我想要一个小的超时时间(33毫秒)。我只想指出,在stackoverflow上找到的一些解决方案在IDLE上不起作用!(我不知道为什么)。您必须在终端上执行它们。而且,我在互联网上找到的最有用的代码是这个:http://home.wlu.edu/~levys/software/kbhit.py。祝你好运! - jespestana
1
这在Windows上不起作用:在Windows下Python中可以使用select()选择文件吗? - Fookatchu
显示剩余11条评论

16

如果你正在使用 Windows,可以尝试以下操作:

import sys, time, 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 

1
这个命令行代码可以正常运行,但在Eclipse中运行时会出现问题,我不确定如何让msvcrt从Eclipse内部读取键盘输入。 - Paul
1
我从未使用过Eclipse进行Python编程,但是我猜它与Sublime Text类似,其内置控制台不是一个合适的实现,只是一个输出。我确信你可以配置Eclipse使用真正的终端或cmd来代替,这样你就可以向其中输入文本。 - m3nda
@Paul - 对于我在 Windows 上不起作用。 我正在运行您的代码,原封不动地更改打印为 py3 语法,并添加 stdout.flush()。 Windows7,python3.6 - some bits flipped
FYI -- 不应该使用 chr 作为变量名来覆盖内置函数。 - kevinsa5
getwche可能对某些人来说是必需的 - v010dya

2
我有一些代码,用tkinter的输入框和按钮制作了一个倒计时应用程序,用户可以输入内容并点击按钮。如果计时器结束,tkinter窗口将关闭并告诉他们时间已经用完。我认为大多数其他解决此问题的方案没有弹出窗口,所以想要添加到列表中 :)
使用raw_input()或input(),因为它会在输入部分停止,直到接收到输入,然后才继续……
我从以下链接中获取了一些代码: Making a countdown timer with Python and Tkinter? 我使用了Brian Oakley的答案来解决这个问题,并添加了输入框等。
import tkinter as tk

class ExampleApp(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        def well():
            whatis = entrybox.get()
            if whatis == "": # Here you can check for what the input should be, e.g. letters only etc.
                print ("You didn't enter anything...")
            else:
                print ("AWESOME WORK DUDE")
            app.destroy()
        global label2
        label2 = tk.Button(text = "quick, enter something and click here (the countdown timer is below)", command = well)
        label2.pack()
        entrybox = tk.Entry()
        entrybox.pack()
        self.label = tk.Label(self, text="", width=10)
        self.label.pack()
        self.remaining = 0
        self.countdown(10)

    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:
            app.destroy()
            print ("OUT OF TIME")


        else:
            self.label.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)

if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()

我知道我添加的内容有点懒惰,但它有效,并且仅是一个示例

这段代码适用于在Windows上使用Pyscripter 3.3


1

对于rbp的回答:

为了处理等于回车符的输入,只需添加一个嵌套条件:

if rlist:
    s = sys.stdin.readline()
    print s
    if s == '':
        s = pycreatordefaultvalue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接