[pygtk] manipulate image from a gtk.Button

Augusto Roccasalva coyotevz at gmail.com
Wed Nov 28 05:31:42 WST 2007


El día Tue, 27 Nov 2007 08:29:02 -0800 (PST)
awalter1 <alain.walter en thalesgroup.com> escribió:

> 
> 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

Yes, too different:
linux 2.6.23 / python 2.5.1 / gtk+ 2.12.1 / pygtk 2.12.0

Anyway .set_image() method is available in pygtk 2.6 and above[0], (I suppose
you have =>pygtk-2.6)
May be you must check that "gtk-button-images" property is True. (Available in
GTK+2.4 and above)

>>> settings = button.get_settings()
>>> settings.get_property("gtk-button-images")
True

> >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('')

And as a last option you can write your own custom button.

# only work when we set stock item via constructor
class CustomButtom(gtk.Button):
    def __init__(self, stock=gtk.STOCK_ADD):
        gtk.Button.__init__(self, stock)
    def set_image_or_label(self, stock_item=None, label=None):
        if stock_item:
            self.set_label(stock_item)
        al = self.get_children()[0]
        hb = al.get_children()[0]
        image, text = hb.get_children()
        if label:
            text.set_text(label)
        else:
            text.set_text('')

[0]http://www.moeraki.com/pygtkreference/pygtk2reference/class-gtkbutton.html#method-gtkbutton--set-image

Excuse me for my english!
Best Regards
--Augusto


More information about the pygtk mailing list