更新时间:2022-05-06 22:12:17
您正在调用 while True
.长话短说,Tk()
有它自己的事件循环.因此,每当您调用某个长时间运行的进程时,它都会阻止此事件循环,您将无能为力.您可能应该使用 after
You're calling while True
. Long story short, Tk()
has it's own event loop. So, whenever you call some long running process it blocks this event loop and you can't do anything. You should probably use after
我在这里避免使用 global
,只是给 window
一个属性.
I avoided using global
here by just giving an attribute to window
.
例如-
import tkinter
def stop():
window.poll = False
def loop():
if window.poll:
print("Polling")
window.after(100, loop)
else:
print("Stopped long running process.")
window = tkinter.Tk()
window.poll = True
window.title("Loop")
startButton = tkinter.Button(window, text = "Start", command = loop)
stopButton = tkinter.Button(window, text = "Pause", command = stop)
startButton.pack()
stopButton.pack()
window.mainloop()