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

Ertl, John john.ertl at fnmoc.navy.mil
Tue Apr 26 19:02:34 CEST 2005


Michael,

Thanks for the understanding and help,  It works kind of.  I was getting
this error with one of my iterations of the code as well.   Do you know what
might be causing this?  Since your code also produced this I figured Oh-Well
and I just put in a try and this at least keeps the error from printing to
the screen.  I will keep trying...a guick google gave me an idea but not
sure.  

Thanks again for the help

Traceback (most recent call last):
  File "./eraseLauncher.py", line 19, in ?
    TK.TKview(newIm,mainTitle="image")
  File "/gpfs3/home/ertlj/BATCH/meteogram/new/test/TKviewTest.py", line 22,
in TKview
    canvas.create_image(0,0,anchor='nw',image=p)
  File "/home/ertlj/ertljVersion/lib/python2.4/lib-tk/Tkinter.py", line
2086, in create_image
    return self._create('image', args, kw)
  File "/home/ertlj/ertljVersion/lib/python2.4/lib-tk/Tkinter.py", line
2075, in _create
    return getint(self.tk.call(
_tkinter.TclError: image "pyimage2" doesn't exist

-----Original Message-----
From: Michael Lange [mailto:klappnase at freenet.de]
Sent: Tuesday, April 26, 2005 01:58
To: Ertl, John
Subject: Re: [Tutor] using TK to view an image and then close the window

On Mon, 25 Apr 2005 14:09:17 -0700
"Ertl, John" <john.ertl at fnmoc.navy.mil> wrote:

> Michael,
>
> I got the TK code from an old post...I really do not know much about how
it
> is supposed to work.  I just thought I seamed so close that it should be
> simple to fix.  I was trying to use self and what not but I still could
not
> get the window to close down.  I guess I will just have to learn a bit
more
> about Tkinter.
>
> Thanks,
>
> John Ertl
>
John,

you are right, it was quite close. It just could be done a little simpler,
like this:

def TKview(img,mainTitle="image"):
       
        top = Tkinter.Tk()

        top.protocol("WM_DELETE_WINDOW", top.quit)# not really necessary if
you don't want to do any cleanup on exit
        top.bind("<q>",lambda event : top.quit)# use lambda here to catch
the event that gets passed by bind()
 
        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()

I didn't test this, but I think it should do pretty much the same as you
expected from the code you posted.

Calling top.destroy() is not really necessary, because python should do this
for you when you quit
the mainloop. However it seems to be "cleaner" to call destroy() explicitely
with a construction like
this (pseudo code):

top = Tkinter.Tk()
top.protocol("WM_DELETE_WINDOW", top.quit)
top.mainloop()
top.destroy()

Using the protocol() method is necessary here, because otherwise clicking
the "Close" button in the window's title bar
would destroy the window, so calling top.destroy() would raise an error.
The last line of this code is only reached, after the mainloop was
interrupted by calling quit() .
What's nice about this construction is, that you can put some
cleanup-on-exit stuff before top.destroy()
that's performed automatically when the window is closed; in a more complex
application with several modules it's
also nice that you can use any (child) Tk widget's quit() method to stop the
mainloop, so
it's not necessary to have a reference to the main Tk() instance in every
module.

Best regards

Michael


More information about the Tutor mailing list