[pygtk] manipulate image from a gtk.Button
awalter1
alain.walter at thalesgroup.com
Wed Nov 28 01:29:02 WST 2007
I have tried your complete example.
A small window is displayed without image in the button.
May be our environment are too different :
unix 11.11 on HPUX
python 2.4.2
gtk+ 2.6.9
>From the time being I'm using this procedure, that seems to work correctly
self.optionButton = gtk.Button(stock=gtk.STOCK_ADD)
SetlabelToButton(self.optionButton,gtk.STOCK_ADD)
...
def SetlabelToButton(button, stock_item=None,label=None):
if (stock_item != None):
button.set_label(stock_item)
alignment = button.get_children()[0]
hboxtemp = alignment.get_children()[0]
image, text = hboxtemp.get_children()
if (label != None):
text.set_text(label)
else:
text.set_text('')
regards
Augusto Rocca... wrote:
>
> El día Tue, 27 Nov 2007 05:56:52 -0800 (PST)
> awalter1 <alain.walter at 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
> _______________________________________________
> pygtk mailing list pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>
>
--
View this message in context: http://www.nabble.com/manipulate-image-from-a-gtk.Button-tf4880483.html#a13973904
Sent from the Gtk+ - Python mailing list archive at Nabble.com.
More information about the pygtk
mailing list