Reputation: 31
I have this script that execute a long running function out of/before the main class/loop in tkinter and there is a button i created to quit the program completely using root.destroy() but it close the gui and the function keep running in the console or even as a background process after i created the executable file.
How to solve this issue?
snippets of my script:
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()
Upvotes: 0
Views: 5354
Reputation: 487
Is there a way I could implement it in one line?
I am using this so far:
b_start = Button(app,text='Start',padx=8, pady=20, command=lambda:threading.Thread(target=filters).start())
Upvotes: 0
Reputation: 13729
Make the thread a daemon to have it die when the main thread dies.
def downloadbutton():
t = threading.Thread(target=download)
t.daemon = True
t.start()
For example:
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()
Upvotes: 6