gpt4 book ai didi

python - windows 认为 tkinter 没有响应

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:14 25 4
gpt4 key购买 nike

我在 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 语句,它就会卡住并进入“无响应”状态,我在想是否有办法防止这种情况发生。

最佳答案

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

关于python - windows 认为 tkinter 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29898519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com