1

My gif is freeze '-' (not displayed/animated, but works out of tkinter). I tried so hard all other ways but still not work, the code:

    import tkinter as tk
    from tkinter import *

    def animate(self):

        global pic

        pic = PhotoImage(file="/home/arnaldo/Desktop/b.gif")

        print("called1")

        self.canvas = Canvas(width=350, height=233, bg='white')
        self.canvas.pack(expand=YES, fill=BOTH)             
        self.canvas.create_image((0, 0), image=pic, anchor=NW)

    class run(tk.Tk):

        def __init__(self, *args, **kwargs):

            tk.Tk.__init__(self, *args, **kwargs)

            animate(self)

    run().mainloop()
halfer
  • 19,824
  • 17
  • 99
  • 186
Radagast
  • 509
  • 10
  • 23

2 Answers2

4

Tkinter doesn't automatically show animated gifs. You will have to do the animation yourself by loading each frame in a loop.

See Play Animations in GIF with Tkinter

If your problem is that you're not seeing any image at all, it's probably due to the image being garbage-collected.

See Cannot Display an Image in Tkinter

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

I solved my problem, here's the code, you have to split the gif (http://gifmaker.me/exploder/), and add images to a folder(gifBackgroundDirectory) and run...:

import tkinter as tk
from tkinter import Canvas, PhotoImage
import os

#YOUR IMAGE FOLDER, THAT CONTAINS tmp-0.gif, tmp-1.gif
gifBackgroundDirectory = "/gif/"

def animate(self):

    #CHECK IF LIST IS EMPTY
    if len(self.gifBackgroundImages) == 0:
        #CREATE FILES IN LIST
        for foldername in os.listdir(gifBackgroundDirectory):
            self.gifBackgroundImages.append(foldername)
        #ALPHABETICAL ORDER
        self.gifBackgroundImages.sort(key = lambda x: int(x.split('.')[0].split('-')[1]))

    if self.atualGifBackgroundImage == len(self.gifBackgroundImages):
        self.atualGifBackgroundImage = 0

    self.background["file"] = gifBackgroundDirectory + self.gifBackgroundImages[self.atualGifBackgroundImage]
    self.label1["image"] = self.background
    self.atualGifBackgroundImage += 1

    #MILISECONDS\/ PER FRAME
    self.after(100, lambda: animate(self))

class run(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        self.label1 = tk.Label(self)
        self.label1.pack()

        #SET ANIMATEDBACKGROUND
        self.gifBackgroundImages = list()
        self.atualGifBackgroundImage = 0
        self.background = tk.PhotoImage()
        animate(self)

run().mainloop()
Radagast
  • 509
  • 10
  • 23