Python Tkinter Threading - How to end / kill / stop thread when user closes GUI
Asked Answered
S

1

0

If I have a non for loop threaded task being run alongside tkinter such as time.sleep(seconds) how can I safely end the task before it has concluded, or, before the time.sleep() has ended.

If you run the program and click the button and then close the GUI window then finished will still print in the interpreter window a few seconds later.

Please note that in this case time.sleep() is just a placeholder for a long running non for loop function.

# python 3 imports
import tkinter as tk
import threading
import queue
import time

def center_app(toplevel):
    toplevel.update_idletasks()
    w = toplevel.winfo_screenwidth() - 20
    h = toplevel.winfo_screenheight() - 100
    size = tuple(int(Q) for Q in toplevel.geometry().split("+")[0].split("x"))
    toplevel.geometry("%dx%d+%d+%d" % (size + (w / 2 - size[0] / 2, h / 2 - size[1] / 2)))

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.protocol("WM_DELETE_WINDOW", self.USER_HAS_CLOSED_WINDOW)
        self.b1 = tk.Button(self, text = "*** Start Thread ***", font = ("Arial",25,"bold"), command = self.func_1)
        self.b1.pack(side = "top", fill = "both", expand = True)
        center_app(self)
        self.queue = queue.Queue()

    def USER_HAS_CLOSED_WINDOW(self, callback = None):
        print ("attempting to end thread")
        # end the running thread
        self.destroy()

    def func_1(self):
        self.b1.config(text = " Thread Running ")
        self.b1.update_idletasks()
        t = ThreadedFunction(self.queue).start()
        self.after(50, self.check_queue)

    def check_queue(self):
        try:
            x = self.queue.get(0) # check if threaded function is done
            self.b1.config(text = "*** Start Thread ***")
            self.b1.update_idletasks()
        except:
            self.after(50, self.check_queue)

class ThreadedFunction(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self, daemon = True)
        self.queue = queue
    def run(self):
        time.sleep(10) # how to end this if user closes GUI
        print ("finished")
        self.queue.put("result")

a = app()
a.mainloop()

I have made a function USER_HAS_CLOSED_WINDOW to catch the user closing the window, where I was thinking I would put something to end the running thread, but I'm not sure how.

I guess the other option is to put an appropriate timeout on the thread and use .join() somehow. But I'm not sure how to do that either

Serriform answered 4/2, 2018 at 18:1 Comment(0)
P
1

It appears that you should use Event object and use wait() method and not time.sleep when you use thread that you want to interrupt.

Probably this can help you:

  1. https://stackoverflow.com/a/5205180
  2. https://stackoverflow.com/a/38828735
Proteus answered 4/2, 2018 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.