Search code examples
pythontkintergifanimated-giftkinter-canvas

Tkinter: Moving a Gif on a canvas without PIL


I have a issue in tkinter for python 3. I would like to create an animating game character in python, tkinter without using PIL. I found a way to animate the character using a gif, but I do not know how to move the gif I tried to use canvas.move here is my code:

from tkinter import *
import os
import time
root = Tk()
c = Canvas(root,width = 500,height = 500)
c.pack()
frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenIDLE.gif")),format = 'gif -index %i' % (i)) for i in range(2)]
def update(ind):
    frame = frames[ind]
    ind += 1
    if ind >= 2:
        ind = 0
    label.configure(image=frame)
    root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
c.move(frames,0,-100)
root.update()
root.mainloop()

Solution

  • move is a method for Canvas, and its first argument needs to be an item on Canvas.

    In your case frames is not an item on the Canvas.

    Replace:

    def update(ind):
        #...
        label.configure(image=frame)
        root.after(100, update, ind)
    label = Label(root)
    label.pack()
    

    with:

    def update(ind):
        #...
        c.itemconfig(character, image=frame)
        c.move(character, 1, 1)
        root.after(100, update, ind)
    character = c.create_image((47,47), image=frames[0])
    

    To convert your label into an image item in Canvas and move it.

    Example

    Below is a complete example that downloads(you can comment download_images out after the initial run) .gif images below online:

    813

    and then moves an image while animating between the two:

    try:                        # In order to be able to import tkinter for
        import tkinter as tk    # either in python 2 or in python 3
    except ImportError:
        import Tkinter as tk
    
    
    def download_images():
        # In order to fetch the image online
        try:
            import urllib.request as url
        except ImportError:
            import urllib as url
        url.urlretrieve("https://i.sstatic.net/57uJJ.gif", "13.gif")
        url.urlretrieve("https://i.sstatic.net/8LThi.gif", "8.gif")
    
    
    def animate_and_move(i):
        i = (i + 1) % 2
        canvas.itemconfig(moving_image, image=canvas.images[i])
        canvas.move(moving_image, 1, 1)
        canvas.after(100, animate_and_move, i)
    
    
    if __name__ == '__main__':
        download_images() # comment out after initial run
        root = tk.Tk()
        canvas = tk.Canvas(root, height=644, width=644, bg='#ffffff')
        canvas.images = list()
        canvas.images.append(tk.PhotoImage(file="8.gif"))
        canvas.images.append(tk.PhotoImage(file="13.gif"))
        moving_image = canvas.create_image((164, 164), image=canvas.images[0])
        animate_and_move(0)
        canvas.pack()
        root.mainloop()
    

    Note that if:

    import tkinter
    tkinter.TkVersion >= 8.6
    

    returns True then .png files are also supported without an additional library.