4

This is the code, really display the gif, but is ugly... i tried to use PIL, but is the same bug. follow the 2 codes (1st my own, 2nd by net) and the print:

gif file (is a random file to test, so cute right? kk):

link_1

please, see the screenshots to understand:

screenshot_1

screenshot_2

1st code:

import tkinter as tk
from tkinter import Canvas, PhotoImage

x = 0

def animate(self):
    global x
    if x == 32:
        x = 0
    else:
        self.pic["format"] = "gif -index " + str(x)
        self.label1["image"] = self.pic
        x += 1

    print(x)

    self.after(50, lambda: animate(self))


class run(tk.Tk):

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

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

        self.pic = PhotoImage(file="/home/arnaldo/Desktop/b.gif", format = "gif -index 0")

        self.label1 = tk.Label(width=350, height=233)
        self.label1.pack(expand="yes", fill="both")

        animate(self)

run().mainloop()

2nd code that i found in net:

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, '/home/arnaldo/Desktop/a.gif')
anim.pack()

def stop_it():
    anim.after_cancel(anim.cancel)

Button(root, text='stop', command=stop_it).pack()

root.mainloop()

both run uglys gifs...

Radagast
  • 509
  • 10
  • 23
  • Where can we find the actual image you're using? The one that's on your computer: `/home/arnaldo/Desktop/b.gif`, maybe the problem lies within that file. – Michiel Overtoom Sep 16 '15 at 08:02
  • in: http://i.imgur.com/DfQqM.gif and i edited the post and up the gif – Radagast Sep 16 '15 at 08:09
  • http://stackoverflow.com/questions/28518072/play-animations-in-gif-with-tkinter – Eric Levieil Sep 16 '15 at 08:50
  • By the way, did you search for `tkinter display animated gif` on Google? – Eric Levieil Sep 16 '15 at 08:51
  • Yes, I tried everything, and all ready code that I found is the same, the gif is damaged, but normal gif is not the problem. My problem is not how to run gif, but why he is damaged as in the images above... – Radagast Sep 16 '15 at 09:01
  • I tried running both your example programs, with the animated dragon picture you provided. The first example indeed shows erroneously decoded GIF frames, but the second example works flawlessy on my system! (OSX Yosemite 10.10.5, python 2.7.10, Pillow 2.9.0). – Michiel Overtoom Sep 17 '15 at 09:49

0 Answers0