我正在尝试一个GUI应用程序,在这个应用程序中,我使用线程来创建并发执行的任务。代码如下:
from tkinter import *
from threading import *
import time
kill = False
def mainer():
global kill
while not kill:
maintext.set(value='bbb')
def quitfunc():
global kill
kill = True
time.sleep(2)
root.destroy()
root=Tk()
maintext=StringVar(value='aaa')
Thread(target=mainer).start()
root.protocol("WM_DELETE_WINDOW", quitfunc)
root.mainloop()
问题:
mainer
线程添加一个无限循环来打印isalive()
,它也会一直说True
。为什么它不退出?mainer
函数中添加print(kill)
语句,则会得到两个结果之一:
maintext.set()
语句之上,则在退出窗口时,打印停止执行,但线程仍然没有退出。很少有这样的情况,我假设这取决于执行quit函数时函数所在的行。在我不知道这里发生了什么事。感谢任何帮助。在
如果将线程设为守护进程,则当主线程终止时,它将消亡,因此您根本不需要任何退出逻辑:
相关问题 更多 >
编程相关推荐