0

I want to display a gif in a GUI I am making using tkinter.I have written the code which shows the gif on the GUI , but I am not able to stop it after a given time

def add_gif(window, gif, place_x, place_y, time_after_which_it_should_destroy=3000):
    def destroy():
        print("destroying...")
        label.destroy()

    def count(main_gif):
        print("counting..")
        file = Image.open(main_gif)
        print("done")
        return file.n_frames

    frameCount = count(gif)
    frames = [PhotoImage(file=gif, format='gif -index %i' % i) for i in
              range(frameCount)]  # iterating over the given number of frames

    def update(ind):
        print(f"updating..{ind}")
        frame = frames[ind]
        ind += 1
        if ind == frameCount:
            ind = 0
        label.configure(image=frame)
        window.after(100, update, ind)

    print("running..")
    label = Label(window)
    print("label made")
    label.place(x=place_x, y=place_y)
    print("placed")
    window.after(time_after_which_it_should_destroy, destroy, update)

I want it to stop showing itself after a given time period

MrHola21
  • 301
  • 2
  • 8
  • One way would be to keep track of the time somehow in the `update()` function and have it stop calling `window.after()` when the given time period has elapsed. Another way would be to save the identifier the `after()` method returns and somewhere else in the program call `window.after_cancel(id)` to cancel further calls to `update()` after the desired time period. – martineau May 08 '21 at 15:37
  • Also see accepted answer to question [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). – martineau May 08 '21 at 15:55

0 Answers0