Trying to display a whole image in tkinter

3.2k Views Asked by At

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.stack.imgur.com/PSCce.png

desired result is https://i.stack.imgur.com/ByFHv.jpg

2

There are 2 best solutions below

1
On BEST ANSWER

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

0
On

You can anchor your image to 'north-west' and set the coordinate to 0,0, or like furas said "Expand your canvas to full window". For example using this code you should be able to get the full display:

from tkinter import *

root = Tk()
root.geometry('300x300')

photo = PhotoImage(file = './Images/img1.gif')
root.geometry("450x450")
root.update()

canvas = Canvas(root)
canvas.pack(expand=YES, fill=BOTH)

img = canvas.create_image(0,0, anchor="nw", image=photo)
root.after(20000, lambda: canvas.delete(img))


root.mainloop()