0

When a new thread destroys a tkinter Tk object whose mainloop is in the main thread, the new thread seems to get stuck and never continues or completes. Why does this happen? The code below shows an example of what I mean:

from tkinter import *
from threading import *
import time

win = Tk()


def exit_after_time():
    time.sleep(2)
    win.destroy()


thread = Thread(target=exit_after_time)
thread.start()

win.mainloop()
print("mainloop quit")
thread.join()
print("thread joined")

When this is run, the mainloop is quit after 2 seconds, but the thread is never joined. At the moment I'm just working around this by making the thread a daemon and not waiting for it to finish, but I'd like to know what's actually going on.

0 Answers0