Sar*_*son 0 python multithreading tkinter python-multithreading python-2.7
我有这个脚本,它在 tkinter 中的主类/循环之外/之前执行一个长时间运行的函数,并且我创建了一个按钮来使用 root.destroy() 完全退出程序,但它关闭了 gui 并且该函数继续运行创建可执行文件后,控制台甚至作为后台进程。
如何解决这个问题?
我的脚本片段:
from tkinter import *
import threading
def download():
#downloading a video file
def stop(): # stop button to close the gui and should terminate the download function too
root.destroy()
class
...
...
...
def downloadbutton():
threading.Thread(target=download).start()
Run Code Online (Sandbox Code Playgroud)
使线程成为守护进程,使其在主线程死亡时死亡。
def downloadbutton():
t = threading.Thread(target=download)
t.daemon = True
t.start()
Run Code Online (Sandbox Code Playgroud)
例如:
import tkinter as tk
import threading
import time
def download():
while True:
time.sleep(1)
print('tick tock')
def stop(): # stop button to close the gui and should terminate the download function too
root.destroy()
def downloadbutton():
t = threading.Thread(target=download)
t.daemon = True
t.start()
root = tk.Tk()
btn = tk.Button(text = "Start", command=downloadbutton)
btn.pack()
btn = tk.Button(text = "Stop", command=stop)
btn.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)