任何人都知道 python 会在 input()
处停止或暂停,这使得很难在超时时获取输入,这是可能的:
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()
我真正的问题是为什么代码会在输入
处暂停,主要是这样做有什么好处?
当然,如果我们可以解决这个问题(对于我认为的任何事情),那么让代码保持这样的状态是愚蠢的。欢迎所有意见,请发表您的看法。
最佳答案
也许应该有一个内置函数来禁用暂停。这将使多线程变得更加容易,但是当您必须测试使用输入创建的某些变量时,暂停会很方便:
input1 = input("enter a big number")
if input1 >= 8:
print("That is a big number")
else:
print("That is tiny...")
如果在没有暂停的情况下运行,您将收到错误 input1 is not Defined,因此暂停至关重要。希望这有帮助。
关于python - 为什么代码在 python 中停止在 input() 处 - 有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25092619/