I want to stop the function animation
when the function stop
finish his job (represented by sleep(5)).
When I click on the button my application freeze and nothing work.
If i change command=start
by command=animation
in the button, the animation work perfectly.
import tkinter as tk
import threading
import os
from time import sleep
#* VAR ------------------------------------
i = 1
j = 0
anim = True
width = 300
height = 300
cur_dir = os.path.dirname(__file__)
picture = os.path.join(cur_dir,"settings.png")
#* FUNCTION ------------------------------------
def start():
th_1.start()
th_2.start()
th_1.join()
th_2.join()
def animation():
global i,j
if anim :
if i:
canvas.move(image_canvas,1,0)
app.after(30,animation)
j += 1
if j > 6:
j = i = 0
else :
canvas.move(image_canvas,-1,0)
app.after(30,animation)
j += 1
if j > 6:
j,i = 0,1
def stop():
global anim
sleep(5)
anim = False
#* THREAD ------------------------------------
th_1 = threading.Thread(target = animation)
th_2 = threading.Thread(target = stop)
#* TKINTER ------------------------------------
app = tk.Tk()
app.geometry("600x400")
app.config(bg="#333")
btn = tk.Button(app,text="animation",height=2,command=start)
btn.pack()
image = tk.PhotoImage(file=picture).subsample(2)
canvas = tk.Canvas(app,width=width,height=height,bg="#333",bd=0,highlightthickness=0)
image_canvas = canvas.create_image(width/2,height/2,image=image)
canvas.pack()
app.mainloop()
Also if I change this two function by :
def animation():
while anim :
print("salut")
sleep(1)
def stop():
global anim
sleep(5)
anim = False
everything work perfectly
I also try : ( whith and whithout print("Thread on")
)
def start():
th_1 = threading.Thread(target = animation)
th_1.start()
#print("Thread on")
th_1.join()
print("done")
but it's not working