The code below describes a confirmation popup function with Tk. The goal is that when the message box pops up, concurrently another thread is counting down from 120 seconds. Once the countdown is over, I want the Tk window to automatically shut and continue. My problem is that Tk has issues multithreading, and I have no idea on how to get around this.
def showConfirmationPopup():
forceClose = False
def trigger():
forceClose = True
root.destroy()
root = tk.Tk()
root.withdraw() # Hide the main window
root.iconbitmap(default='blank.ico')
countdown_thread = threading.Thread(target=countdown,args=(root,trigger,))
countdown_thread.start()
result = messagebox.askyesno(XXX)
if forceClose:
result = True
if not result:
XXX
return result
def countdown(result,close):
t = 120
while t > 0:
t -= 1
time.sleep(1)
close()
When the other thread is executed and the countdown finishes you can see the result below.
Exception in thread Thread-1 (countdown):
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\xxx\script.py", line 15, in countdown
close()
File "C:\Users\xxx\script.py", line 42, in trigger
root.destroy()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2368, in destroy
self.tk.call('destroy', self._w)
Any help is appreciated. :)