tkinter exit/quit/killing 函数线程退出主循环

tkinter exit/quit/killing function threading out of mainloop

我有这个脚本在 tkinter 中执行 of/before 主要 class/loop 的长 运行 函数 我创建了一个按钮来使用 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()

使线程成为守护进程,使其在主线程死亡时死亡。

def downloadbutton():
    t = threading.Thread(target=download)
    t.daemon = True
    t.start()

例如:

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()

有没有一种方法可以在一行中实现它?

目前我正在使用这个:

b_start = Button(app,text='Start',padx=8, pady=20, command=lambda:threading.Thread(target=filters).start())