Tkinter Button image option

E

Elaine Jackson

When I try to put an image onto a Tkinter button, I get a message that says the
image in question "doesn't exist" (it does, though). One of the references I
looked at said that Tkinter has to be "configured for" the Python Imaging
Library in order for the 'image' option to work. I've got the PIL, but as for
"configuration", I don't know. Maybe I need to reinstall Tcl/Tk? If anyone can
point me to the appropriate resource, I'd be very much obliged.
Peace
 
J

Jason Harper

Are you saving a reference to the image you loaded somewhere (perhaps as
an attribute of the button, if that's the only user of the image)?
Merely using an image in a Tkinter widget is NOT sufficient to keep it
from being garbage collected.

PIL is not required for basic use of images in Tkinter, although it does
give you lots more options for working with them.
Jason Harper
 
E

Elaine Jackson

<snip>
| Sorry it's a german site.

Kein Problem. Ganz im Gegenteil: ich habe den genau richtigen Hinweis gefunden.
Danke vielmals!
 
F

Fredrik Lundh

Elaine said:
When I try to put an image onto a Tkinter button, I get a message that says the
image in question "doesn't exist" (it does, though). One of the references I
looked at said that Tkinter has to be "configured for" the Python Imaging
Library in order for the 'image' option to work. I've got the PIL, but as for
"configuration", I don't know. Maybe I need to reinstall Tcl/Tk? If anyone can
point me to the appropriate resource, I'd be very much obliged.

did you get an error similar to this:

TclError: image "pyimage1" doesn't exist

a common reason for this is that you have multiple Tk instances in
your application.

if you create a PhotoImage under one Tk instance, you cannot access
it from other instances.

to fix this, make sure you only use one Tk instance (use Toplevel to create
new toplevel windows), or make sure that you create the image under the
same instance as the widget you're going to use it in. the easiest way to
do
this is to pass in a master keyword argument to the PhotoImage constructor:

photo = PhotoImage(..., master=myroot)
button = Button(myroot, ...)
button.photo = photo # keep a reference

(if the file you're trying to load doesn't exist,

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top