trying to achieve an automatic open -> close in tkinter

110 Views Asked by At

so i'm doing a project for school with tkinter what i'm currently trying to do is display an image that is a 450x450px .gif

what it needs to do is open > 20sec delay > close

here is the current code

    photo = tkinter.PhotoImage(file = './Images/img1.gif')
    root.geometry("450x450")
    root.update()
    canvas.create_image(225,225, image=photo)
    root.mainloop()

https://i.stack.imgur.com/PSCce.png is the current result

1

There are 1 best solutions below

5
On BEST ANSWER

Save the return value of the create_image (item id), then use that value when you delete the image using canvas.delete.

photo = tkinter.PhotoImage(file = './Images/img1.gif')
root.geometry("450x450")
root.update()
img = canvas.create_image(225,225, image=photo)
root.after(20000, lambda: canvas.delete(img)) # 20,000 milli seconds = 20 seconds
root.mainloop()

Using after, you can do some stuff after specified time.