Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103834
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2016-03-01 18:35 -0800 |
| Message-ID | <8e9f1a84-cced-435e-a379-e1e2ac03f483@googlegroups.com> (permalink) |
| Subject | How to know if an object is still be referenced? |
| From | jfong@ms4.hinet.net |
Recently I was puzzled by a tkinter problem. The codes below (from a book) can display the picture correctly.
gifdir = "../gifs/"
from tkinter import *
win = Tk()
photo = PhotoImage(file=gifdir + "ora-pp.gif")
Button(win, image=photo).pack()
win.mainloop()
And the codes below (from another book) will also work.
class DrumMachine:
....
....
def create_play_bar(self):
....
....
photo = PhotoImage(file='images/signature.gif')
label = Label(playbar_frame, image=photo)
label.image = photo
label.grid(row=start_row, column=50, padx=1, sticky='w')
....
....
In the second example, I noticed that the "photo" was referenced two times and I think it might be a redundancy so I remove the line "label.image = photo". But it fails then.
How can it be? one works and the other not.
I search for answers on the web and here are some links talking about it:
http://stackoverflow.com/questions/20812579/why-it-shows-blank-instead-of-picture-in-my-tkinter-program
http://effbot.org/tkinterbook/photoimage.htm
They all said that you have to keep a reference to the "photo" or else it will be garbage collected. Now, as I had learn Python so far, I know that a keyword argument passed in an object instantiation will bind this object to its attribute name, i.e. create a reference to this object. (and it should or the whole language will be in disaster)
Can anyone help me out?
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
How to know if an object is still be referenced? jfong@ms4.hinet.net - 2016-03-01 18:35 -0800
Re: How to know if an object is still be referenced? Terry Reedy <tjreedy@udel.edu> - 2016-03-02 02:03 -0500
Re: How to know if an object is still be referenced? jfong@ms4.hinet.net - 2016-03-02 03:35 -0800
Re: How to know if an object is still be referenced? sohcahtoa82@gmail.com - 2016-03-02 15:38 -0800
Re: How to know if an object is still be referenced? jfong@ms4.hinet.net - 2016-03-02 18:36 -0800
csiph-web