1

I create a GUI application with gif inside. However, during the course of the work, the gif begins to fail.

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

frames = []
for i in range(61):
    frames.append(PhotoImage(file='bell.gif', format='gif -index ' + str(i)))

def update(ind):
    try:
        frame = frames[ind]
    except IndexError:
        ind = 1
        frame = frames[ind]

    ind += 1

    label.configure(image=frame, bg="white")
    label.pack()
    root.after(100, update, ind)

label = Label(root, bg="white")

root.after(100, update(1))

root.mainloop()

Here is the original gif: https://im4.ezgif.com/tmp/ezgif-4-e5132d340f75.gif

And what happens at the end: https://s3.gifyu.com/images/glitch9af7397783b31f35.gif

martineau
  • 119,623
  • 25
  • 170
  • 301
Wedyarit
  • 31
  • 3
  • Your changes to `ind` from inside `update` will be lost. See https://stackoverflow.com/q/986006/5987 – Mark Ransom Aug 31 '19 at 14:31
  • @Mark: They're not lost because the updated value is passed as an argument in the call to `root.after()` at the end of that function. – martineau Aug 31 '19 at 14:37
  • 1
    Doggy4: Your code seems basically OK. I think the problem has something to do with the GIF image you're using — so I suggest trying another. There's one named "Small global" you can get from [here](https://web.archive.org/web/20150302051639/http://www.animationlibrary.com:80/sc/50/Planet_Earth/?page=6). When I use it instead of your `bell.gif`, there's no problem.. – martineau Aug 31 '19 at 16:22
  • The problem is solved installing Pillow package – Hello World Aug 31 '19 at 16:58

2 Answers2

1

UPDATED You have to incorporate Pillow on packages and works.

enter image description here

enter image description here

ANOTHER WAY FOUND

from tkinter import *
from PIL import Image, ImageTk

class MyLabel(Label):
    def __init__(self, master, filename):
        im = Image.open(filename)
        seq =  []
        try:
            while 1:
                seq.append(im.copy())
                im.seek(len(seq)) # skip to next frame
        except EOFError:
            pass # we're done

        try:
            self.delay = im.info['duration']
        except KeyError:
            self.delay = 100

        first = seq[0].convert('RGBA')
        self.frames = [ImageTk.PhotoImage(first)]

        Label.__init__(self, master, image=self.frames[0])

        temp = seq[0]
        for image in seq[1:]:
            temp.paste(image)
            frame = temp.convert('RGBA')
            self.frames.append(ImageTk.PhotoImage(frame))

        self.idx = 0

        self.cancel = self.after(self.delay, self.play)

    def play(self):
        self.config(image=self.frames[self.idx])
        self.idx += 1
        if self.idx == len(self.frames):
            self.idx = 0
        self.cancel = self.after(self.delay, self.play)


root = Tk()
anim = MyLabel(root, 'bell.gif')
anim.pack()
root.mainloop()
Hello World
  • 207
  • 1
  • 12
0

To avoid glitch in rendering

#temp.paste(image) <-skip this

frame = image.convert('RGBA') <-change temp to image

User689
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – xlmaster Feb 22 '23 at 12:46