1
我想用开始按钮创建一个简单的Python GUI(在Tkinter中),在一个线程中运行一个while循环,并且停止按钮来停止while循环。从tkinter gui停止python线程
我遇到了停止按钮问题,一旦单击开始按钮,停止按钮不会停止任何事情并冻结GUI。
见下面的代码:
import threading
import Tkinter
class MyJob(threading.Thread):
def __init__(self):
super(MyJob, self).__init__()
self._stop = threading.Event()
def stop(self):
self._stop.set()
def run(self):
while not self._stop.isSet():
print "-"
if __name__ == "__main__":
top = Tkinter.Tk()
myJob = MyJob()
def startCallBack():
myJob.run()
start_button = Tkinter.Button(top,text="start", command=startCallBack)
start_button.pack()
def stopCallBack():
myJob.stop()
stop_button = Tkinter.Button(top,text="stop", command=stopCallBack)
stop_button.pack()
top.mainloop()
不知道如何解决这个问题?我相信这是微不足道的,必须做好几千次,但我自己找不到解决方案。
感谢 大卫