Need Tkinter help - Label with image

Matthew Dixon Cowles matt at mondoinfo.com
Sat Jan 12 22:37:16 CET 2002


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