[Tkinter-discuss] Animation using image sequence

david.giesen at kodak.com david.giesen at kodak.com
Tue Dec 22 19:42:11 CET 2009


Hello Nemes:

The issue with the code as written is that the infinite while loop begins 
execution before the root.mainloop() ever gets called (so root.mainloop 
never gets called....).

To start the animation AFTER the root.mainloop() call, I've moved the 
animation work into a function and used the after method to call that 
function after the mainloop initiates.

Also, instead of using a while loop with a sleep call (which hangs the 
program while waiting), I've changed the animation function to call itself 
each time it finishes.  This way the program doesn't hang while animating 
the picture, which is useful if you want this to be part of a larger 
program.

Hope this is helpful!

Dave Giesen



#Working on window interface of soundAsleep

#Tkinter will handle the GUI
from Tkinter import *

root = Tk()
root.title("soundAsleep beta")

frame=[]

#Importing the images. They are named w1.gif, w2.gif...w7.gif
for i in range(1,7):
    fname="core/working/w"+str(i)+".gif"
    frame+=[PhotoImage(file=fname)]

wrap = Canvas(root, width=200, height=120)
wrap.pack()

def do_animation(currentframe):
        def do_image():
                wrap.create_image(100,70,image=frame[currentframe], 
tag='ani')
        # Delete the current picture if one exists
        wrap.delete('ani')
        try:
                do_image()
        except IndexError:
                # End of image list reached, start over at the first image 
- works for an arbitrary number of images
                currentframe = 0
                do_image()
        wrap.update_idletasks() #Force redraw
        currentframe = currentframe + 1
        # Call myself again to keep the animation running in a loop
        root.after(5000, do_animation, currentframe)
 
# Start the animation loop just after the Tkinter loop begins
root.after(10, do_animation, 0)

root.mainloop()

David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D 
| 
Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 | 
david.giesen at kodak.com | 1-585-588-0480 Office | 
www.kodak.com 




From:
Nemes Andrei <teh_sh_meister at yahoo.com>
To:
tkinter-discuss at python.org
Date:
12/22/2009 10:47 AM
Subject:
[Tkinter-discuss] Animation using image sequence
Sent by:
tkinter-discuss-bounces+david.giesen=kodak.com at python.org



Hi!

I'm trying to make an animation using a sequence of gif images and the 
canvas widget, but all I see when I compile is a black screen. What I'm 
trying to do is delete the previous image and create the next one each 
frame. Am I doing it wrong? Is it do-able at all?
Help would be greatly appreciated. Thank you.

#Working on window interface of soundAsleep

#Tkinter will handle the GUI
from Tkinter import *
import time

root = Tk()
root.title("soundAsleep beta")

frame=[]
currentframe=0

#Importing the images. They are named w1.gif, w2.gif...w7.gif
for i in range(1,7):
    fname="core/working/w"+str(i)+".gif"
    frame+=[PhotoImage(file=fname)]

wrap = Canvas(root, width=200, height=120)
wrap.pack()

#trying to make the animation
while True:
    ani=wrap.create_image(100,70,image=frame[currentframe])
    time.sleep(5)
    wrap.update_idletasks() #Force redraw
    wrap.delete(ani)
    if (currentframe==5):
        currentframe=0
    else:
        currentframe+=1
 
root.mainloop()

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20091222/313e56be/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 826 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20091222/313e56be/attachment.gif>


More information about the Tkinter-discuss mailing list