Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Animations in GIF with Tkinter [duplicate]

I've been trying to play an animated gif using Tkinter.PhotoImage, but haven't been seeing any success. It displays the image, but not the animation. The following is my code:

root = Tkinter.Tk()
photo = Tkinter.PhotoImage(file = "path/to/image.gif")
label = Tkinter.Label(image = photo)
label.pack()
root.mainloop()

It displays the image in a window, and that's it. I'm thinking that the issue has something to do with Tkinter.Label but I'm not sure. I've looked for solutions but they all tell me to use PIL (Python Imaging Library), and it's something that I don't want to use.

With the answer, I created some more code (which still doesn't work...), here it is:

from Tkinter import *

def run_animation():
    while True:
        try:
            global photo
            global frame
            global label
            photo = PhotoImage(
                file = photo_path,
                format = "gif - {}".format(frame)
                )

            label.configure(image = nextframe)

            frame = frame + 1

        except Exception:
            frame = 1
            break

root = Tk()
photo_path = "/users/zinedine/downloads/091.gif"

photo = PhotoImage(
    file = photo_path,
    )
label = Label(
    image = photo
    )
animate = Button(
    root,
    text = "animate",
    command = run_animation
    )

label.pack()
animate.pack()

root.mainloop()

Thanks for everything! :)

like image 280
Zizouz212 Avatar asked Feb 14 '15 17:02

Zizouz212


People also ask

Can I use GIF in tkinter?

This little script lets you use animated GIFs in tkinter label.

How do you repeat a GIF?

Loop a part of an animated GIF image Or you can right-click and choose 'Split'. Right-click on the other clips and select 'Delete'. You can now add the same GIF file and repeat the process to create a loop.

Is Lottie better than GIF?

So, how do Lottie files compare to GIFs? Well for starters, they are 600% smaller than GIFs and are 10x faster to ship. Since they are tiny files, they won't use up as much disk space and they increase download speed, which is better for conversion rates.

Why is GIF better than Apng?

APNG files support 24 bit color as well as 24 bit transparency. A GIF file has an 8 bit transparency. What that means is that APNG files can handle color better and look a lot smoother when transparent. This is because GIF files have an 8 bit color palette which can give a grainy appearance.


2 Answers

Here's a simpler example without creating an object:

from tkinter import *
import time
import os
root = Tk()

frameCnt = 12
frames = [PhotoImage(file='mygif.gif',format = 'gif -index %i' %(i)) for i in range(frameCnt)]

def update(ind):

    frame = frames[ind]
    ind += 1
    if ind == frameCnt:
        ind = 0
    label.configure(image=frame)
    root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()
like image 31
Alex Avatar answered Nov 15 '22 05:11

Alex


You have to drive the animation yourself in Tk. An animated gif consists of a number of frames in a single file. Tk loads the first frame but you can specify different frames by passing an index parameter when creating the image. For example:

frame2 = PhotoImage(file=imagefilename, format="gif -index 2")

If you load up all the frames into separate PhotoImages and then use timer events to switch the frame being shown (label.configure(image=nextframe)). The delay on the timer lets you control the animation speed. There is nothing provided to give you the number of frames in the image other than it failing to create a frame once you exceed the frame count.

See the photo Tk manual page for the official word.

like image 200
patthoyts Avatar answered Nov 15 '22 03:11

patthoyts