0

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

Dorian MB
  • 1
  • 1
  • This is because you cant interact from outside the loop without breaking the loop. There are many questions about [tkinter and threading](https://stackoverflow.com/questions/26703502/threads-and-tkinter/26703844#26703844) take the time to read it. – Thingamabobs Oct 31 '20 at 19:32
  • Does this answer your question? [Threads and tkinter](https://stackoverflow.com/questions/26703502/threads-and-tkinter) – Thingamabobs Oct 31 '20 at 19:34
  • I try to edit my function `start` after reading [tkinter and threading](https://stackoverflow.com/questions/26703502/threads-and-tkinter/26703844#26703844) but it doesn’t work. – Dorian MB Nov 01 '20 at 15:23
  • I cant find a reason why you do need threading here. Why you think you need it? – Thingamabobs Nov 01 '20 at 15:27
  • Be reminded that tkinter is not thread-safe. Calling `join()` may interfere tkinter mainloop. Try removing the two `join()`. Also better using while loop instead of `after()` in a thread task. – acw1668 Nov 02 '20 at 04:30
  • Removing the two `join()` work thank you !! but i can do only one time my programme (i guess it's because of the thread still working), i try to call a new function `joint_()` at the end of `stop()` with `th_1.join()` and `th_2.join()` inside but didnt work. Also I try to remove `after()` by a while loop and `sleep(0.3)` but the animation didnt start. – Dorian MB Nov 03 '20 at 22:13

0 Answers0