Search code examples
pythontkinterphotoimage

trying to achieve an automatic open -> close in tkinter


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.sstatic.net/PSCce.png is the current result


Solution

  • 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.