jpeg files displayed in a loop

Gary Herron res0ne7x at
Tue Dec 11 19:48:47 EST 2001

On Monday 10 December 2001 06:24 am, Fernando Pérez wrote:
> Kevin Cazabon wrote:
> > # ok, probably a couple minor typos, but it should be that simple.
> Well, you got me curious. Here is the code with some things corrected. This
> actually runs, but it hangs at the first image. My cpu usage pegged at 100%
> and I just had to kill the job, it wasn't going anywhere. Any ideas?
> #!/usr/bin/env python
> import Tkinter
> import Image, ImageTk
> import os
> dir = "/usr/local/home/fperez/local/python/jpegs"
> def showimage():
>     image = files.pop()
>     files.append(image)
>     image = ImageTk.PhotoImage( #ok, do try/except
>     b.configure(image=image)
>     b.update_idletasks()
>     b.after(1000, showimage)
> a = Tkinter.Tk()
> b = Tkinter.Label(a)
> b.pack()
> files = os.listdir(dir) # ok, check them for file types...
> os.chdir(dir)
> while 1:
>     showimage()

Ouch! This gives you (an attempt at) infinitely many calls to
showimage, EACH of which tries to schedule yet more calls with its
call to "after".

Get rid of the while loop.  One initial call to showimage is enough --
it will schedule further calls at the appropriate interval.

P.S.  How do you plan to stop the scheduling of calls to showimage.
As it stands, when the "files" list is empty, you get an error (from
the files.pop call).  If i remember correctly, such errors are caught
by Tkinter, a traceback is printed and execution is continued, so you
get a non-ending sequence of error messages, one per second.  You
should probably test for the existance of an image before you attempt
to pop and display it, and certainly before you reschedule another
call to showimage.

More information about the Python-list mailing list