Tkinter wart: bug or feature?

David Eppstein eppstein at ics.uci.edu
Fri Nov 15 23:08:40 EST 2002


In article <mailman.1037407464.4609.python-list at python.org>,
 Chad Netzer <cnetzer at mail.arc.nasa.gov> wrote:

> That's not how I took it.  Assuming you actually meant "image1 = None", then 
> that would explicitly lose the reference to the Tkinter.Image object, which 
> happens anyway once the function returns and the 'image1' variable goes out 
> of scope.
> 
> What Mike was saying is that:
> 
> lbl = Tkinter.Label(win.window, image=image1)
> 
> Hands off image1 to the Label object, which eventually becomes a Tcl/Tk 
> construct.  However, this does NOT increase the reference count for the 
> Tkinter.Image object itself, and so when image1 goes out of scope, it gets a 
> reference count of zero, and causes the actual image memory to be deleted, 
> which causes Tk to not display the image (because the Tcl/Tk delete command 
> was called on it)

Ok, thanks for the clarification.

So the workaround would be to have a global
dontDelete = []
and then in your function add dontDelete.append(image1)

...or do all the ugly bookkeeping necessary to make sure that image1 
gets deleted when and only when it is no longer needed, which is what I 
thought Python was supposed to save us from having to do.

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list