Changing the Image on a button

O

odeits

I want to be able to toggle if the button has an image or text. For
some reason the following code gets the button to have an image but
when i push the button i expect the image to go away but it does not.
Am I missing something?

from Tkinter import *


def do():
btn.configure(image = None)

root = Tk()
img1 = PhotoImage(file="bacon.gif")

btn = Button(image = img1, command = do, text = "hello" )
btn.img = img1
btn.pack()
root.mainloop()
 
O

odeits

 >> from Tkinter import *
 >>
 >> def do():
 >>     btn.configure(image = None)
 >>
 >> root = Tk()
 >> img1 = PhotoImage(file="bacon.gif")
 >>
 >> btn = Button(image = img1, command = do, text = "hello" )
 >> btn.img = img1
 >> btn.pack()
 >> root.mainloop()
 >>

Try this change:

  from: btn.configure(image = None)
    to: img1.blank()

-John

E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.11770http://www.pctools.com/en/spyware-doctor-antivirus/

This does in fact clear the image out, however it isn't causing the
text to display... Do i have have to create a new button and swap it
out?
 
J

John Posner

Try this change:
I knew you were gonna ask that! :) I haven't worked in this area before,
but I
found this at http://effbot.org/tkinterbook/button.htm:

In earlier versions of Tkinter, the image option overrides the text
option.
If you specify both, only the image is displayed. In later versions, you
can
use the compound option to change this behavior. To display text on top
of
an image, set compound to CENTER:

b = Button(master, text="Click me", image=pattern, compound=CENTER)

So here's a reworking, in which the text and image are both toggled by
pressing the button:

### button that toggles both text and image
from Tkinter import *

def pushed():
"""callback: button push"""

global toggle

if toggle:
btn.config(text="", image=img_empty)
toggle = not toggle
else:
btn.config(text=msg, image=img_good)
toggle = not toggle

### main program
toggle = True
msg = "hello"

root = Tk()

### store two versions of an image in global variables:
# 1. original image
img_good = PhotoImage(file="bacon.gif")

# 2. blank image, created by copying and erasing
img_empty = img_good.copy()
img_empty.blank()

### create toggle button
btn = Button(root, compound=CENTER,
text="hello", font="helvetica 14 bold",
image=img_good,
command=pushed)
btn.pack()

### go
root.mainloop()





E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.11780
http://www.pctools.com/en/spyware-doctor-antivirus/
 
O

odeits

 >> > Try this change:
 >> >
 >> >   from: btn.configure(image = None)
 >> >     to: img1.blank()
 >> >
 >>
 >> This does in fact clear the image out, however it isn't causing the
 >> text to display... Do i have have to create a new button and swap it
 >> out?

I knew you were gonna ask that! :) I haven't worked in this area before,
but I
found this athttp://effbot.org/tkinterbook/button.htm:

    In earlier versions of Tkinter, the image option overrides the text
option.
    If you specify both, only the image is displayed. In later versions, you
can
    use the compound option to change this behavior. To display text on top
of
    an image, set compound to CENTER:

     b = Button(master, text="Click me", image=pattern, compound=CENTER)

So here's a reworking, in which the text and image are both toggled by
pressing the button:

### button that toggles both text and image
from Tkinter import *

def pushed():
    """callback: button push"""

    global toggle

    if toggle:
        btn.config(text="", image=img_empty)
        toggle = not toggle
    else:
        btn.config(text=msg, image=img_good)
        toggle = not toggle

### main program
toggle = True
msg = "hello"

root = Tk()

### store two versions of an image in global variables:
# 1. original image
img_good = PhotoImage(file="bacon.gif")

# 2. blank image, created by copying and erasing
img_empty = img_good.copy()
img_empty.blank()

### create toggle button
btn = Button(root, compound=CENTER,
            text="hello", font="helvetica 14 bold",
            image=img_good,
            command=pushed)
btn.pack()

### go
root.mainloop()

E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.11780http://www.pctools.com/en/spyware-doctor-antivirus/

Thank you very much for your help! It is now behaving the way I wanted!
 

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
474,264
Messages
2,571,323
Members
48,007
Latest member
Elvis60357

Latest Threads

Top