python - 为什么代码在 python 中停止在 input() 处 - 有什么好处?

标签 python python-3.x input timeout

任何人都知道 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/

相关文章:

python - Flask-SQLAlchemy - 检索 Query.all() - 每行中的所有列

python-3.x - 在 Python 中签署 SAML 响应

python - Tornado 发送和接收字节

html - 输入越过 div

c - 将输入限制为 4 位数字条目 (0-9)

python - 当数据流入 python 时,将数据输出到文本文件中

python - 如何在 setup.py 中运行 Makefile?

python - 如何避免使用opencv和numpy逐像素循环遍历图像

python-3.x - Groupby id 并在大矩阵 (3x3 mio.) 上展开(或求和)

c - ANSI C 中的无缓冲 I/O