I'm trying a GUI application in which I am using threading to create concurrently executed tasks. Here is the code:
from tkinter import *
from threading import *
import time
kill = False
def mainer():
global kill
while not kill:
maintext.set(value='bbb')
def quitfunc():
global kill
kill = True
time.sleep(2)
root.destroy()
root=Tk()
maintext=StringVar(value='aaa')
Thread(target=mainer).start()
root.protocol("WM_DELETE_WINDOW", quitfunc)
root.mainloop()
ISSUES:
- As it currently stands, on closing the root window, the process doesn't stop. It keeps running. Even if I add an infinite loop to print
isalive()
for themainer
thread, it keeps sayingTrue
. Why does it not quit? - In case I add a
print(kill)
statement in themainer
function, I get one of the two outcomes:- If written above the
maintext.set()
statement, on quitting the window, the print stops getting executed but the thread still does not quit. Very very rarely, it does, which I am assuming must depend on which line the function was on when the quit function was executed. - If written below that statement, on quitting, the thread almost inevitably quits.
- If written above the
I have no clue what is happening here. Any help is appreciated.