我想做一个raw_input('Enter something: .')
。我想让它睡3秒钟,如果没有输入,则取消提示并运行代码的其余部分。然后代码再次循环并执行raw_input
。如果用户输入'q'之类的东西,我也希望它能够打破。raw_input和timeout
回答
我需要留下来远离信号。信号不能与我的程序一起工作,因为我必须对其他python脚本和os.system命令进行如此多的调用,以至于我无法跟踪我所处的程序。我需要的是程序要求用户输入并3秒后,如果没有输入继续,然后再问这个问题,最后我希望它永远运行,如果他们没有输入。 – ykmizu 2010-08-12 20:06:05
有不(明确至少不)使用线程一个简单的解决办法:用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]:很明显,这won't work on Windows,因为select()的底层实现需要套接字,并且sys.stdin不是。感谢您的提醒,@Fookatchu。
如果你在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
这将在命令行上正常工作,但在Eclipse中运行时不起作用,我不确定如何获取msvcrt从Eclipse内部键盘。 – Paul 2010-10-21 21:15:34
我从来没有使用Eclipse的Python,但我会猜测这是相同的sublimtext,内置控制台不是一个适当的实现,只是一个输出。我相信你可以配置Eclipse在这种情况下使用真正的终端或cmd,所以你可以输入输入。 – erm3nda 2017-01-02 10:52:52
@Paul - 这在Windows上不适用于我。我正在运行你的代码,除了改变打印为py3语法,并添加stdout.flush()之外。 Windows7,python3.6 – mike 2017-01-23 16:49:44
对于RBP的回答是:
为了考虑到回车输入等于简单地添加一个嵌套条件:
if rlist:
s = sys.stdin.readline()
print s
if s == '':
s = pycreatordefaultvalue
我有一些代码,使一个tkinter的输入框和按钮倒数应用程序,使他们可以输入的东西,并点击按钮,如果计时器用完tkinter风ow关闭并告诉他们他们耗尽时间。 我认为这个问题的大多数其他解决方案没有弹出窗口,所以认为id添加到列表:)
与raw_input()或input(),它是不可能的,因为它停止在输入部分,直到它接收输入,然后进行......
我采取从以下链接一些代码: Making a countdown timer with Python and Tkinter?
我用布莱恩·奥克利的回答这个问题,并且增加了entrybox等
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. jquery autosuggestion和timeout
- 2. raw_input,字典和类
- 3. Python,输入和raw_input
- 4. AngularJS $ scope。$ on()和$ timeout()
- 5. d3 timeout like angular .timeout
- 6. HAProxy,“timeout tunnel”vs“timeout client/server”
- 7. 为什么`mask_`中和`timeout`?
- 8. akka http配置中的idle-timeout和request timeout有什么区别?
- 9. LocationListener timeout
- 10. rqworker timeout
- 11. file_get_contents timeout
- 12. DirectoryEntry Timeout
- 13. HttpWebRequest.GetRequestStream()Timeout
- 14. HttpClientParams timeout
- 15. WebClient TimeOut
- 16. Python Timeout
- 17. 'rbuf_fill'中的救援:Timeout :: Error(Timeout :: Error)
- 18. Raw_input子流程
- 19. 强制的raw_input
- 20. Python:改变raw_input?
- 21. argv vs. raw_input
- 22. Python:扫描raw_input
- 23. 用的raw_input
- 24. Python raw_input(“”)错误
- 25. 在python中使用curses和raw_input
- 26. raw_input中的字母和数字计数
- 27. 语法问题 - if/else和raw_input()
- 28. if语句的raw_input和Python中
- 29. 异步循环和raw_input问题
- 30. MobileServiceClient invokeApi timeout
相关:Python 3的定时输入/ 15528939](https://开头stackoverflow.com/questions/15528939/python-3-timed-input) – n611x007 2015-03-13 13:10:16
相关:[超时在Python函数调用/492519](https://stackoverflow.com/questions/492519/timeout-on-a-python-函数调用) – n611x007 2015-03-13 13:10:36
相关:[如何设置输入时间限制/2933399](https://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-input) – n611x007 2015-03-13 13:11:09