[pygtk] manipulate image from a gtk.Button
Augusto Roccasalva
coyotevz at gmail.com
Wed Nov 28 00:18:36 WST 2007
El día Tue, 27 Nov 2007 05:56:52 -0800 (PST)
awalter1 <alain.walter en thalesgroup.com> escribió:
>
> the button.set_image (newImage) has no effect !
>
> Matías Alejandro Torres wrote:
> >
> > awalter1 escribió:
> >> At the creation of the button, I use :
> >> image = gtk.Image()
> >> image.set_from_stock (gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
> >> button = gtk.Button()
> >> button.add (image)
> >> On the 'changed' callback, I need to change the stock item to
> >> STOCK_REMOVE
> >> and the next time to STOCK_ADD.
> >>
> > Maybe if you set the image with the set_image method,
> >
> > image = gtk.Image()
> > image.set_from_stock (gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
> > button = gtk.Button()
> > button.set_image (image)
> >
> > And when you want to change the image,
> >
> > button.set_image (newImage)
> >
> > If you can't use the set_image method, you'll have to keep a reference to
> > the image you want to remove, remove it, add it to the container, and hide
> > and show the button.
> >
This work for me:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk
win = gtk.Window()
image_add = gtk.Image()
image_rem = gtk.Image()
b = gtk.Button()
image_add.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
image_rem.set_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_BUTTON)
def on_clicked(button):
state = button.get_data('state')
if state == '+':
button.set_image(image_rem)
button.set_data('state', '-')
elif state == '-':
button.set_image(image_add)
button.set_data('state', '+')
else:
raise ValueError("unknown state :(")
# setup initial state
b.set_image(image_add)
b.set_data('state', '+')
# connect signals
b.connect('clicked', on_clicked)
win.connect('delete-event', gtk.main_quit)
win.add(b)
win.show_all()
gtk.main()
Regards
--Augusto
More information about the pygtk
mailing list