How to know if an object is still be referenced?

jfong at ms4.hinet.net jfong at ms4.hinet.net
Wed Mar 2 21:36:31 EST 2016


sohca... at gmail.com at 2016/3/3  UTC+8 7:38:45AM wrote:
> "label" might be a local variable, but it's constructor includes a reference to the frame in which it is going.  The frame object will create a reference to the newly-created Label object.  At that point, there will be two references to the new Label object.  When the function exits and "label" goes out of scope, the object still exists because the frame still has a reference.
> 
It sound reasonable enough:-)

There is a page talking about why should keep a separate reference in tkinter.
http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

I also saw almost every code on the web about it was written in this way:
    label = Label(playbar_frame, image=photo)
    label.image = photo
But why? Won't it be better if it was written as:
    label = Label(playbar_frame, image=photo)
    label.imgKeeper = photo
At least it makes the meaning more clear, not lead someone to a wrong direction:-(

Anyway, following Terry's suggestion might be a better idea, making it bind to an object which will live for as long as the application is.

--Jach



More information about the Python-list mailing list