我在我的簡單示例中創建了一個帶有tKinter的Python GUI我有一個觸發簡單循環的按鈕來增加計數器。我已成功地對計數器進行了線程處理,因此我的GUI不會凍結,但是我遇到了使其停止計數的問題。這是我的代碼:tKinter多線程停止線程
# threading_example.py
import threading
from threading import Event
import time
from tkinter import Tk, Button
root = Tk()
class Control(object):
def __init__(self):
self.my_thread = None
self.stopThread = False
def just_wait(self):
while not self.stopThread:
for i in range(10000):
time.sleep(1)
print(i)
def button_callback(self):
self.my_thread = threading.Thread(target=self.just_wait)
self.my_thread.start()
def button_callbackStop(self):
self.stopThread = True
self.my_thread.join()
self.my_thread = None
control = Control()
button = Button(root, text='Run long thread.', command=control.button_callback)
button.pack()
button2 = Button(root, text='stop long thread.', command=control.button_callbackStop)
button2.pack()
root.mainloop()
我該如何安全地讓計數器停止遞增並優雅地關閉線程?
我應該怎麼做呢? – Vince