PLEASE HELP--Button images refuse to show.

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 22 23:09:46 EDT 2010


En Mon, 19 Apr 2010 12:58:16 -0300, Barrett <barrett.n.b at gmail.com>  
escribió:

> I have been fighting the same bug for weeks now with zero success: I
> am trying to get images to come up on my buttons, but they are way too
> small. Regardless of whether I used my old Python 2.5.1 or now 2.6.5,
> the following code:
>
> [... too much code ...]
>
> Results in the following:
>
> http://img194.yfrog.com/img194/237/weirdness3.jpg
>
> ("3" and above, I have not coded the pix for. I want to get the "1"
> and "2" right before proceeding.)
>
> I am completely at wit's end. I have tried using PIL, I have tried
> resizing the buttons, and zooming the images, and saving the images
> as .gif's and .jpeg's and .bmp's and you name it. Absolutely nothing
> is working. I have a major assignment due TOMORROW and absolutely MUST
> figure this out. Someone please show me what the heck I am doing wrong
> and what I can do to fix it.

I'm afraid this is coming late for your assignment then. For the next time:

- Try to isolate the problem. Start with your current, full program, and  
strip down sections (one at a time) and see whether the problem is still  
present or not. Eventually, you'll get working code (so you know the last  
removed section was the culprit) or it becomes so small that you cannot  
simplify it further.
(You posted a very long program, most of it irrelevant for this question)

- Explain how to reproduce the problem, what you got, and what you  
expected to get. (You posted what you got, but not what you expected. I  
had to *imagine* that those funny squares were supposed to contain an  
image).

This is a heavily stripped down version of your program that still shows  
the problem:

<code>
import Tkinter

def callback():
     b['image'] = image
     b['text'] = ''

root = Tkinter.Tk()
image = Tkinter.PhotoImage(file="1.gif")
b = Tkinter.Button(root, width=3, height=2, command=callback)
b['text'] = 'X'
b.pack()
root.mainloop()
</code>

If you remove the 'width' and 'height' specifications, it works fine. Now  
look at the documentation, from http://effbot.org/tkinterbook/button.htm :

"height= The height of the button. If the button displays text, the size  
is given in text units. If the button displays an image, the size is given  
in pixels (or screen units). If the size is omitted, it is calculated  
based on the button contents. (height/Height)

width= The width of the button. If the button displays text, the size is  
given in text units. If the button displays an image, the size is given in  
pixels (or screen units). If the size is omitted, or zero, it is  
calculated based on the button contents. (width/Width)"

Initially the button contains text, so width=3 and height=2 are in  
character units. When you set an image, the same values are interpreted in  
pixels instead.

Replace the callback function with this one, or some variant:

def callback():
     b.config(image=image, text='',
              width=image.width(),
              height=image.height())

(btw, notice usage of config() to set widget attributes, and implicit line  
continuation instead of \ )

-- 
Gabriel Genellina




More information about the Python-list mailing list