Search code examples
pythonimagetkinterphotoimage

Trying to display a whole image in tkinter


ok so school project

goal of this code piece is to display a 450x450 gif for 20sec

output only displays a section of the gif

    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))
    root.mainloop()

from what i know, geometry defines the window dimensions, and the coordinates in img define the center position.

current result is https://i.sstatic.net/PSCce.png

desired result is https://i.sstatic.net/ByFHv.jpg


Solution

  • Expand your canvas to full window.

    import Tkinter as tkinter
    
    root = tkinter.Tk()
    
    canvas = tkinter.Canvas(root)
    canvas.pack(fill=tkinter.BOTH, expand=1) # expand
    
    photo = tkinter.PhotoImage(file = 'img1.gif')
    root.geometry("450x450")
    root.update()
    
    img = canvas.create_image(225,225, image=photo)
    #root.after(20000, lambda: canvas.delete(img))
    
    root.mainloop()
    

    enter image description here