X-Received: by 10.50.36.68 with SMTP id o4mr3154807igj.7.1456918511823; Wed, 02 Mar 2016 03:35:11 -0800 (PST) X-Received: by 10.50.155.65 with SMTP id vu1mr110537igb.0.1456918511804; Wed, 02 Mar 2016 03:35:11 -0800 (PST) Path: csiph.com!feeder.erje.net!2.us.feeder.erje.net!news.glorb.com!hb3no10668684igb.0!news-out.google.com!k1ni6392igd.0!nntp.google.com!ok5no5670250igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Wed, 2 Mar 2016 03:35:11 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=36.238.103.179; posting-account=G2sM6AoAAADOlDdo9rWD6sFkj3T5ULsz NNTP-Posting-Host: 36.238.103.179 References: <8e9f1a84-cced-435e-a379-e1e2ac03f483@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: How to know if an object is still be referenced? From: jfong@ms4.hinet.net Injection-Date: Wed, 02 Mar 2016 11:35:11 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.python:103862 Terry Reedy at 2016/3/2 UTC+8 3:04:10PM wrote=EF=BC=9A > On 3/1/2016 9:35 PM, jfong@ms4.hinet.net wrote: > > Recently I was puzzled by a tkinter problem. The codes below (from a bo= ok) can display the picture correctly. > > > > gifdir =3D "../gifs/" > > from tkinter import * > > win =3D Tk() > > photo =3D PhotoImage(file=3Dgifdir + "ora-pp.gif") > > Button(win, image=3Dphoto).pack() > > win.mainloop() >=20 > Since photo is a global name, the binding remain until you explicitly=20 > delete it or exit the app. >=20 > > And the codes below (from another book) will also work. > > > > class DrumMachine: > > .... > > .... > > def create_play_bar(self): > > .... > > .... > > photo =3D PhotoImage(file=3D'images/signature.gif') > > label =3D Label(playbar_frame, image=3Dphoto) > > label.image =3D photo > > label.grid(row=3Dstart_row, column=3D50, padx=3D1, sticky= =3D'w') > > .... > > .... >=20 > Here photo is a local name and the binding disappears when the function= =20 > exits. I would rewrite it to follow pattern 1. >=20 > self.photo =3D PhotoImage(file=3D'images/signature.gif') > label =3D Label(playbar_frame, image=3Dself.photo) >=20 > To me, saving an attribute reference is not worth the extra line. >=20 > > In the second example, I noticed that the "photo" was referenced two ti= mes > > and I think it might be a redundancy so I remove the line "label.image = =3D photo". But it fails then. >=20 > On another question, I made the same suggestion. Oops. >=20 > --=20 > Terry Jan Reedy Thanks, Terry. After reading your reply I noticed that I had make a mistake= . The "image" is not an attribute of the Button. It's an option. The "label= .image=3Dphoto" does a different thing. But it didn't help on solving my pu= zzle. If the problem was caused by the "photo" is a local, then binding the "phot= o" to an local attribute ("label" is a local too) seems no meaning at all. = But it did make difference! No idea how the underneath mechanism works. I run this example under the pdb and it can display the picture correctly e= ven without the "label.image=3Dphoto" statement. Why it fails on running at= real time? tk event scheduling? I don't know. --Jach