Newbie Tkinter Question

Peter Otten __peter__ at web.de
Thu Apr 14 02:08:10 EDT 2005


Pseud O'Nym wrote:

> the following fragment doesn't cause any errors and results in buttons the
> correct size for the images, but the buttons don't work, and the images
> aren't displayed. If I add a text property, and remove the images, they
> work fine, so the rest of my code's OK. I've searched this group and
> Python.org to no avail.
> 
> class App:
>   def __init__(self, master):
>     frame = Frame(master)
>     frame.pack(side=LEFT, fill=Y)
>     image1 = PhotoImage(file='button_a.gif')
>     self.button = Button(frame, image=image1)
>     self.button.pack(side=TOP)

      self.image = image1

> Can anyone enlighten me?

For reasons that are beyond me widgets do not increase the reference count
of a PhotoImage. Therefore you have to put a reference to the image
elsewhere, e. g. into the App instance, to prevent it from being
garbage-collected when the image1 variable goes out of scope.

Peter




More information about the Python-list mailing list