0

I am new to Python and I want to run a GIF by loading and displaying multiple frames consecutively to create the effect of movement, how can I achieve this?

I get a result, but it only displays the first frame (image).

This is my code so far:

from tkinter import *
import time 
# creating the canvas, size in pixels
canvas = Canvas(width = 600, height = 400, bg = '#33EDAE')
# pack the canvas into a frame/form
canvas.pack(expand = YES, fill = BOTH)
# load the .gif image file
gif1 = PhotoImage(file = 'Frame01.jpg')
gif2 = PhotoImage(file = 'Frame02.jpg')
gif3 = PhotoImage(file = 'Frame03.jpg')
gif4 = PhotoImage(file = 'Frame04.jpg')
gif5 = PhotoImage(file = 'Frame05.jpg')
gif6 = PhotoImage(file = 'Frame06.jpg')
gif7 = PhotoImage(file = 'Frame07.jpg')
gif8 = PhotoImage(file = 'Frame08.jpg')
gif9 = PhotoImage(file = 'Frame09.jpg')
gif10 = PhotoImage(file = 'Frame10.jpg')
gif11 = PhotoImage(file = 'Frame11.jpg')
gif12 = PhotoImage(file = 'Frame12.jpg')
# put gif image on canvas
# Center image's axis
canvas.create_image(685, 350, anchor = CENTER, image = gif1)
canvas.create_image(685, 350, anchor = CENTER, image = gif2)
canvas.create_image(685, 350, anchor = CENTER, image = gif3)
canvas.create_image(685, 350, anchor = CENTER, image = gif4)
canvas.create_image(685, 350, anchor = CENTER, image = gif5)
canvas.create_image(685, 350, anchor = CENTER, image = gif6)
canvas.create_image(685, 350, anchor = CENTER, image = gif7)
canvas.create_image(685, 350, anchor = CENTER, image = gif8)
canvas.create_image(685, 350, anchor = CENTER, image = gif9)
canvas.create_image(685, 350, anchor = CENTER, image = gif10)
canvas.create_image(685, 350, anchor = CENTER, image = gif11)
canvas.create_image(685, 350, anchor = CENTER, image = gif12)
# run it ...
time.sleep(0.1)
frame2 = PhotoImage(file=Frame01.jpg, format="gif -index 2")
time.sleep(0.1)
frame3 = PhotoImage(file=Frame02.jpg, format="gif -index 3")
time.sleep(0.1)
frame4 = PhotoImage(file=Frame03.jpg, format="gif -index 4")
time.sleep(0.1)
frame5 = PhotoImage(file=Frame04.jpg, format="gif -index 5")
time.sleep(0.1)
frame6 = PhotoImage(file=Frame05.jpg, format="gif -index 6")
time.sleep(0.1)
frame7 = PhotoImage(file=Frame06.jpg, format="gif -index 7")
time.sleep(0.1)
frame8 = PhotoImage(file=Frame07.jpg, format="gif -index 8")
time.sleep(0.1)
frame9 = PhotoImage(file=Frame08.jpg, format="gif -index 9")
time.sleep(0.1)
frame10 = PhotoImage(file=Frame09.jpg, format="gif -index 10")
time.sleep(0.1)
frame11 = PhotoImage(file=Frame10.jpg, format="gif -index 11")
time.sleep(0.1)
frame12 = PhotoImage(file=Frame11.jpg, format="gif -index 12")
time.sleep(0.1)
frame13 = PhotoImage(file=Frame12.jpg, format="gif -index 13")
mainloop()
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 1
    You cannot use `time.sleep` for this, you need to change the displayed image within the Tk loop. Have a look at [How to create a timer using tkinter?](https://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter). – jdehesa May 30 '18 at 14:35

1 Answers1

0

No worries, I have figured out a solution to my problem using turtle: Thank you for the help. Hopefully this can also help you if you have a similar problem. :)

import turtle
import time

#Define the 'playgif' operator which will be used to run the code later on
def playgif(frame):
    screen = turtle.Screen()
    screen.setup(700,700)
    screen.bgpic(frame)
    screen.exitonclick
    turtle.bgcolor('#33EDAE')

#load the images and assign them each a variable
frame1 = "Frame01.jpg"
frame2 = "Frame02.jpg"
frame3 = "Frame03.jpg"
frame4 = "Frame04.jpg"
frame5 = "Frame05.jpg"
frame6 = "Frame06.jpg"
frame7 = "Frame07.jpg"
frame8 = "Frame08.jpg"
frame9 = "Frame09.jpg"
frame10 = "Frame10.jpg"
frame11 = "Frame11.jpg"
frame12 = "Frame12.jpg"


#run the code by calling each frame and delaying each frames exposure
again = 1
while again == 1:
    playgif(frame1)
    time.sleep(0.08)
    playgif(frame2)
    time.sleep(0.08)
    playgif(frame3)
    time.sleep(0.08)
    playgif(frame4)
    time.sleep(0.08)
    playgif(frame5)
    time.sleep(0.08)
    playgif(frame6)
    time.sleep(0.08)
    playgif(frame7)
    time.sleep(0.08)
    playgif(frame8)
    time.sleep(0.08)
    playgif(frame9)
    time.sleep(0.08)
    playgif(frame10)
    time.sleep(0.08)
    playgif(frame11)
    time.sleep(0.08)
    playgif(frame12)
    time.sleep(0.08)
mainloop()