Tkinter Button image option

Fredrik Lundh fredrik at pythonware.com
Fri Apr 16 07:05:38 EDT 2004


Elaine Jackson wrote:

> 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>







More information about the Python-list mailing list