[Tutor] Images + Tkinter

Magnus Lycka magnus@thinkware.se
Sun Nov 17 15:29:02 2002


Speculation to follow...

At 17:50 2002-11-17 +0000, alan.gauld@bt.com wrote:
>I feel stupid. I must be missing something...

Extensive expertise in the internals of Tcl/Tk?

> >     canvas.create_image(200, 250, image=photo)
>
>Why doesn't this retain a reference to the image and stop it
>being dropped?

I don't know. If you look in Tkinter.py, you see that it
will be handed over to tk (if I understand this) with a
call like this (in Canvas._create):

         return getint(apply(
             self.tk.call,
             (self._w, 'create', itemType)
             + args + self._options(cnf, kw)))

The keyword argument "image=photo" is in kw.

Python doesn't keep any reference to it. Does Tcl/Tk
use some kind of weak reference? Obviously it will
know what image to work with if the image object doesn't
go out of scope. <guess category="wild">Maybe this has
something to do with not leaking memory if you swap
images?</guess>

> >     button=Tkinter.Button(frame, text="EXIT", command=tk.destroy)
> >     button.pack()
>
>In fact all of these objects should go out of scope at the end of
>the function except for the fact they are parented on tk which is
>global. So why does the image die but not the button?

I'm not Tkinter expert, but I think it's basically the same
as with wxPython. In that case there are C++ references that
makes things live on even though the seem to have gone away
in the Python code. Here there are "secret" things happening
in Tcl/Tk I guess.

This kind of magic sometimes cause newbies to get a very warped
image of how classes work in Python, that you don't need to keep
references to them, but just create and throw away. I think I
whined about that some weeks ago on this very list...

>Still confused.

Not Python's fault! ;)
Blame Osterhaut! :)


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se