[Tutor] using TK to view an image and then close the window

Michael Lange klappnase at freenet.de
Mon Apr 25 22:42:53 CEST 2005


On Mon, 25 Apr 2005 10:24:08 -0700
"Ertl, John" <john.ertl at fnmoc.navy.mil> wrote:


Hi John,

> I can get the image to show
> up and minimize and maximize but I can not get the window to close and then
> display the next image.  
> 
> I have tired several things but no luck. 
> 
> Any help on getting the TK window to shutdown cleanly would be appreciated.
> Thanks
> 
> John Ertl 
> 
> <code> # this is supposed to display an image in a TK window but the close
> (X) does not work.
> import Image
> import Tkinter,ImageTk
> 
> def TKview(img,mainTitle="image"):
>         
>         app = Tkinter.Tk()
>         app.withdraw()
> 
>         top = Tkinter.Toplevel(app,visual="truecolor",colormap="new")
>         top.title(mainTitle)
>         top.protocol("WM_DELETE_WINDOW", quit)
>         top.bind("<q>",quit)
>         top.bind("<Q>",quit)
> 
>         canvas = Tkinter.Canvas(top)
>         canvas.pack()
> 
>         p = ImageTk.PhotoImage(img)
> 
>         canvas['width'] = img.size[0]
>         canvas['height'] = img.size[1]
> 
>         canvas.create_image(0,0,anchor='nw',image=p)
> 
>         top.mainloop()
> 
> def quit(event=None):
>         top.destroy()
>         top.quit()
> </CODE>

If that's all of the code, "top" is created as a local variable inside your TKview() function,
so your quit() function doesn't know about it.
Second, I don't see much use for using a second Toplevel window at all, why not just pack the
canvas onto the main Tk() window?


> 
> <CODE># this code gets the images opens them and calls the TK code above to
> display them
> 
> import glob
> import thread
> import Image
> 
> import TKviewTest # the module to view the images
> 
>        
> gifList = glob.glob("./*.gif")
> print gifList
> for image in gifList:
>        image = image[2:] # glob leaves ./ in file name
> 
>        newIm    = Image.open(image)
> 
>        TK = TKviewTest
>        thread.start_new_thread(TK.TKview(newIm,mainTitle="image"))
> 
> </CODE>

I hope this helps

Michael



More information about the Tutor mailing list