Hi,

I'm trying to pin an image to a button, using the 'image' option.
I've looked about on the net and it seems the best way to do it is to create the image in a PhotoImage object. I've done this and then stuck it onto the button but for some reason when it's running, the button assumes the dimensions of the image but it's not displayed, and the button freezes, doesn't respond.

self.Button = Button(self, image=PhotoImage(file="image.gif"))

any help greatly appreciated thanks

a1eio

Keyword variable image=object needs an object not a function call. You need to use
image1=PhotoImage(file="image.gif")
and then
self.Button = Button(self, image=image1)

I LOVE YOU!!!!!!

hehe, thanks it works now.

a1eio

Hi, I have a little doubt...:)

Keyword variable image=object needs an object

I agree..

not a function call. You need to use
image1=PhotoImage(file="image.gif")
and then
self.Button = Button(self, image=image1)

I was wondering is PhotoImage is a class or function?
when this following statement can return an object,

image1=PhotoImage(file="image.gif")

why not here..

self.Button = Button(self, image=PhotoImage(file="image.gif"))

Please correct if im wrong.

thank you,
regards,
kath.

hehe, i agree with you, which is why i made the mistake in the first place and didn't even notice it. But yea, that'd be a nice answer cause i havn't a clue.

thanks,
a1eio

from Tkinter import *
import inspect

# find out if Tkinter's PhotoImage is a class
print inspect.isclass(PhotoImage)  # True

The other problem with Tkinter is that you are going back and forth between two languages, TCL and Python, so Python rules don't always apply.

Right. The rule is (I learned this from Vega!) that a permanent reference to the image must be maintained. Otherwise, Tkinter loses track of it.

So this line

self.Button = Button(self, image=PhotoImage(file="image.gif"))

just won't work because as soon as your function ends, the parameter 'image' goes out of scope and gets forgotten.

Jeff

hi, bumpsfeld

from Tkinter import *
import inspect
# find out if Tkinter's PhotoImage is a class
print inspect.isclass(PhotoImage)

that was a nice snippet, thanks.

hey, jrcagle
thanks for your reply too. Your reply was convincing.. thanks a lot.

regards,
kath.

Ah HA!
Very well explained jrcagle, thanks a bunch.

a1eio

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.