I sumarize my problem through this piece of code. When I end my program by closing the tkinter main window, I need the whole program ends, but the loops goes on executing until the functions is over. I suppose there is a way to force these functions ends too. I think there is a way to detect the program was ended, so I could end the functions.
import threading
import time
from tkinter import *
def loop1_10():
for i in range(1, 11):
time.sleep(1)
print(i)
def loop1_10_b():
for i in range(1, 11):
time.sleep(2)
print(i)
threading.Thread(target=loop1_10).start()
threading.Thread(target=loop1_10_b).start()
MainWindow = Tk()
MainWindow.mainloop()
CodePudding user response:
The other way to handle this is to make the threads "daemons". A daemon thread will be forcibly closed when the app exits; it doesn't block the app.
threading.Thread(target=loop1_10, daemon=True).start()
threading.Thread(target=loop1_10_b, daemon=True).start()
Note that I'm not saying one is better or worse than the other. Each option has its uses.
CodePudding user response:
Add a protocol, WM_DELETE_WINDOW
, to your MainWindow
, where you use define a function you defined, on_close()
that gets called once the tkinter window is closed.
The on_close()
function will redefine the global variable end
from False
into True
, and in each for
loop, if the end
variable's value is True
, return
out of them:
import threading
import time
from tkinter import *
def loop1_10():
for i in range(1, 11):
if end:
return
time.sleep(1)
print(i)
def loop1_10_b():
for i in range(1, 11):
if end:
return
time.sleep(2)
print(i)
end = False
def on_closing():
global end
end = True
MainWindow.destroy()
threading.Thread(target=loop1_10).start()
threading.Thread(target=loop1_10_b).start()
MainWindow = Tk()
MainWindow.protocol("WM_DELETE_WINDOW", on_closing)
MainWindow.mainloop()
But there is still a problem with the above code; if the end = True
happened right before the time.sleep()
call(s), the last time.sleep()
(s) will still make the program wait for a second or two before terminating.
To fix this, use time.time()
and a while
loop to manually check how much time has passed before continuing each for
loop:
import threading
import time
from tkinter import *
def loop1_10():
for i in range(1, 11):
old_time = time.time()
while True:
if end:
return
if time.time() - old_time < 1:
continue
break
print(i)
def loop1_10_b():
for i in range(1, 11):
old_time = time.time()
while True:
if end:
return
if time.time() - old_time < 2:
continue
break
print(i)
end = False
def on_closing():
global end
end = True
MainWindow.destroy()
threading.Thread(target=loop1_10).start()
threading.Thread(target=loop1_10_b).start()
MainWindow = Tk()
MainWindow.protocol("WM_DELETE_WINDOW", on_closing)
MainWindow.mainloop()
But do note from this comment by @kindall:
use time.time() and a while loop
-- don't do this, it'll use up an entire CPU core waiting for the loop to exit. this will not only eat battery unnecessarily, but since Python only uses one CPU core due to the Global Interpreter Lock, it will make the rest of the program sluggish, too