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

Ertl, John john.ertl at fnmoc.navy.mil
Mon Apr 25 19:52:03 CEST 2005


I forgot to mention that I am running on a Linux system if that makes any
difference for the TK part.

Thanks, 

John Ertl 

-----Original Message-----
From: Ertl, John 
Sent: Monday, April 25, 2005 10:24
To: tutor at python.org
Subject: using TK to view an image and then close the window

All,

I have been struggling with being able to view an image and most things have
not worked for one reason or another so I am trying to fall back on a way I
used to do it in PERL.  Use TK to show the image.  I do not know TKInter but
I have two scripts one that gets a list of images and anouther that displays
them using Tkinter.   This is basically how I would like to use it...I am
going to be using PIL to manipulate the image (and show() does not work) so
I just want a quick view to see how it looks.  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>

<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>


More information about the Tutor mailing list