Tkinter wart: bug or feature?
Chad Netzer
cnetzer at mail.arc.nasa.gov
Mon Nov 18 16:39:50 EST 2002
On Saturday 16 November 2002 06:57, Fredrik Lundh wrote:
> lbl = Tkinter.Label(win.window, image=image1)
> lbl.photo = image1
>
> which guarantees that the image won't be collected before
> the widget itself.
Well, maybe I need more schooling. Could someone look at this code and tell
me where my reasoning goes wrong.:
import Tkinter
def make_photo_label( parent ):
image2 = Tkinter.Image('photo', file="WatchmenLogo.gif")
lbl2 = Tkinter.Label(parent, image=image2)
lbl2.photo = image2
lbl2.pack(expand=1, fill=Tkinter.BOTH)
return
if __name__ == '__main__':
root = Tkinter.Tk()
make_photo_label( root )
root.mainloop()
When make_photo_label() finishes execution, the 'lbl2' variable goes out of
scope, and is deleted from the local namespace. So the Tkinter.Label object
should be 'collected' (which DOESN'T mean that Tk forgets it; the widget is
still packed, and still exists to Tk).
When 'lbl2' is collected, the reference count of 'image2' drops. (?)
Furthermore, when 'image2' it goes out of scope, it drops again. So it
should (seemingly) be collected as well, and the image should become blank.
I guess I don't understand how attaching image2 as an attribute to lbl2
should change anything. lbl2 still gets collected when the function is over,
and in doing so, it's link to 'image2' should be broken, and then 'image2'
should be collected.
However, when running the program, I see that is NOT the case. The image
only disappears if I comment out "bl2.photo = image2". Do I misunderstand
reference counting?
--
Bay Area Python Interest Group - http://www.baypiggies.net/
Chad Netzer
cnetzer at mail.arc.nasa.gov
More information about the Python-list
mailing list