Windows认为tkinter没有回应

时间:2015-04-27 14:27:55

标签: python tkinter

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

   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")

然而,如果没有印刷声明,它只会冻结并进入“不响应”,而我正在徘徊是否有办法阻止它。

1 个答案:

答案 0 :(得分:1)

作为一般规则,您不应该在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()
相关问题