GIF in tkinter glitches

Question:

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

Asked By: Wedyarit

||

Answers:

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()
Answered By: Hello World

To avoid glitch in rendering

#temp.paste(image) <-skip this

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

Answered By: User689
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.