when i try to run the code, the gif displays at a very high speed and only shows the center of the gif. The window size is also much too large once the start button is pressed.
i tried to run this code
import tkinter as tk
from tkinter import *
from PIL import Image
root = tk.Tk()
root.title("Displaying Gif")
#idle animation stuff
Idle = "C:\Users\Andy\Downloads\desktoppetstuff\DesktopPetTvIdle.gif"
IdleInfo = Image.open(Idle)
IdleFrames = IdleInfo.n_frames #num of frames
IdleGifObjects = []
for i in range(IdleFrames):
obj = tk.PhotoImage(file=Idle, format=f"gif -index {i}")
IdleGifObjects.append(obj)
#make anim work
def IdleAnim(CurrentFrame=0):
global loop
image = IdleGifObjects [CurrentFrame]
IdleGifLabel.configure(image=image)
CurrentFrame = CurrentFrame + 1
if CurrentFrame == IdleFrames:
CurrentFrame = 0
loop = root.after(50, lambda: IdleAnim(CurrentFrame))
def StopAnim():
root.after_cancel(loop)
IdleGifLabel = Label(root, image="")
IdleGifLabel.pack()
start = tk.Button(root, text="Start", command=lambda: IdleAnim(CurrentFrame=0))
start.pack()
stop = tk.Button(root, text="Stop", command=StopAnim)
stop.pack()
root.mainloop()
New contributor
Andrew Harrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.