Need Tkinter help - Label with image

Whosit whosit at dev.null
Sat Jan 12 18:06:27 EST 2002


Mathew,

Thank you!

I modified Test-2 to return (l1, img), instead of just return l1, and
it started working.  I have added your response to my "Python quirks"
file.

Thanks again for a lucid and correct answer on a Saturday.

Daryl

On Sat, 12 Jan 2002 13:37:16 -0800, Matthew Dixon Cowles wrote:

> On Sat, 12 Jan 2002 12:35:28 -0800, Daryl P McDaniel
> <python at mc2research.org> wrote:
> 
> Dear Daryl,
> 
>>Hello,
> 
> Hello!
> 
>>I am having a problem with Tkinter that I don't understand.  I have
>>consulted "Python and Tkinter Programming", by John Grayson, and "An
>>Introduction to Tkinter", by Fredrik Lundh, without finding an answer.
> 
>>Basically, I can't get a Label to display an image if both the
>>PhotoImage and Label calls are contained within the same function.  I
>>can make my application work, but don't want to ship it until I
>>understand what is going on.
> 
>>PROBLEM:
>>    When both the PhotoImage and Label function calls are made in the
>>    same function, the Label is blank, but at the correct size.  If the
>>    PhotoImage and Label function calls are not in the same function,
>>    the Label displays the image properly.
> 
> It's an odd feature feature in Tkinter. You need to keep an extra
> reference to your image around to prevent it from being garbage-
> collected. In your examples that work, you do that by making it a
> global. If your Tkinter widgets are wrapped up in Python objects,
> another useful way is to assign the image to an attribute of the object.
> Something like:
> 
>   self.keepReferenceAround=img
> 
> Regards,
> Matt



More information about the Python-list mailing list