windows 认为 tkinter 没有响应

windows thinks tkinter is not responding

我在 python 中有一个小应用程序,它工作正常,除了这个小问题:它应该 运行 连续循环,直到用户通过按钮告诉它停止,但是当我点击开始按钮,windows 告诉我它没有响应。现在,如果我写一个简单的打印语句,一切正常:

   def update(self):
      self.bStart.config(state = 'disabled')
      self.bStop.config(state = 'active')

      self.running = True
      self.toRun = True
      while self.toRun:
          [...do some stuff...]
          time.sleep(self.toWait)
          print("i'm alive")

然而,如果没有 print 语句,它只会冻结并转到 'not responding',我在想是否有办法防止这种情况发生。

作为一般规则,您不应在 GUI 中调用 sleep,也不应使用更​​新显示的循环。如果您定期更新显示,正确使用 after 可以避免循环。

总体思路是编写一个执行单个更新的函数(即:否则您将拥有的循环主体)。然后,让该函数通过 after.

调用自身

这是一个示例,您可以在其中看到条形图随时间增长,而无需使用循环:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self)
        self.start_button = tk.Button(self, text="start", command=self.on_start)
        self.stop_button = tk.Button(self, text="stop", command=self.on_stop)

        self.start_button.pack()
        self.stop_button.pack()
        self.canvas.pack(fill="both", expand=True)

        # call on_stop to initialize the state of the buttons
        self.on_stop()

    def on_start(self):
        """Start the animation"""
        self.canvas.delete("all")
        self.rect_id = self.canvas.create_rectangle(0,0,1,20, fill="blue")

        self.running = True
        self.start_button.configure(state="disabled")
        self.stop_button.configure(state="normal")
        self.draw_one_frame()

    def on_stop(self):
        """Stop the animation"""
        self.start_button.configure(state="normal")
        self.stop_button.configure(state="disabled")
        self.running = False

    def draw_one_frame(self):
        """Draw a single frame of animation"""
        (x0, y0, x1, y1) = self.canvas.coords(self.rect_id)
        if x1 < self.canvas.winfo_width():
            x1 += 1
        else:
            x1 = 1

        self.canvas.coords(self.rect_id, (x0, y0, x1, y1))

        if self.running:
            self.after(10, self.draw_one_frame)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()