can someone explain why ..

Grant Edwards grante at visi.com
Tue Apr 25 21:17:35 EDT 2006


On 2006-04-25, Schüle Daniel <uval at rz.uni-karlsruhe.de> wrote:

>> I'm not familiar with Tkinter, but it seems as thought with 2,
>> the "image" variable is garbage collected after the
>> constructor of Main is called. With 1, you save a reference to
>> the image, so it does not get garbage collected.

Correct.

> image is local variable of imageLabel

Actually it's a parameter to the constructor that was called to
create the "imageLabel" object.

> I would expect that in case imageLabel lives, it should hold
> alife objects bound to its local variables

It would.  Therefore the "image" object apparently isn't bound
to a local variable in the object imageLabel.

> I am just curious *why* reference to image is not hold by
> imageLabel which on his part is hold by frame1...

Good question.  Untold millions of programmers have tripped
over that bug.  Well, would you believe untold dozens?  I know
I did.  It's a common enough problem that it's given special
mention in a couple books on Tkinter.

My _guess_ is that this is one of the misfeatures resulting
from the way Tkinter is implimented: it's not a real binding of
Python to the tk library.  It's a Python wrapper around a TCL
interpreter which is bound to the tk library (or something like
that). This results in some counter-intuitive (non-pythonic)
behaviors like the one you discovered with bitmap images and
labels.

-- 
Grant Edwards                   grante             Yow!  I feel like I'm
                                  at               in a Toilet Bowl with a
                               visi.com            thumbtack in my forehead!!



More information about the Python-list mailing list