[pygtk] Color a ToggleButton

John Finlay finlay at moeraki.com
Sun Sep 6 23:26:07 WST 2009


E.R. Uber wrote:
> As in?
>
>             widget.modify_bg(gtk.STATE_NORMAL, color_off)
> and 
>             widget.modify_bg(gtk.STATE_ACTIVE, color_on)
>
> That doesn't work either.
>
> On Sat, Sep 5, 2009 at 7:41 PM, John Finlay <finlay at moeraki.com 
> <mailto:finlay at moeraki.com>> wrote:
>
>     E.R. Uber wrote:
>
>         After reading PyGTK FAQ 4.6 at
>         http://faq.pygtk.org/index.py?req=show&file=faq04.006.htp
>         <http://faq.pygtk.org/index.py?req=show&file=faq04.006.htp>
>         <http://faq.pygtk.org/index.py?req=show&file=faq04.006.htp
>         <http://faq.pygtk.org/index.py?req=show&file=faq04.006.htp>> I
>         thought perhaps I could change the color of a ToggleButton by
>         copying its style, updating the style, and setting the style.
>         This did not work.
>
>         Then reading PyGTK FAQ 4.16 at
>         http://faq.pygtk.org/index.py?req=show&file=faq04.016.htp
>         <http://faq.pygtk.org/index.py?req=show&file=faq04.016.htp>
>         <http://faq.pygtk.org/index.py?req=show&file=faq04.016.htp
>         <http://faq.pygtk.org/index.py?req=show&file=faq04.016.htp>>
>         it mentions that gtk.Button (and I assume gtk.ToggleButton)
>         are windowless widgets that do not allow changing their
>         background and base color unless you insert them into an
>         EventBox.
>
>
>         Using a parent EventBox succeeds in changing the color of the
>         event box, but not the child ToggleButton, the button just
>         appears to be surrounded by the color change of the event box.
>         I tried the same with a Button and it works the same way.
>
>         I am using pygtk 2.12.1, gtk 2.16.4,  glade 3.6.7, and python
>         2.6 on both windows and fedora core 9. I have only tried this
>         to date on windows though.
>
>     You don't need the EventBox just use toggle.modify_bg() with the
>     appropriate state and color. Note in your example you need to set
>     STATE_NORMAL and STATE_ACTIVE to have different colors for the
>     active and inactive states.
>
>     John
>
>
Works fine on ubuntu 9.04 (Python 2.6.2, PyGTK 2.14.1 (Gtk+ 2.16.1)) and 
Fedora 10.21 (Python 2.5.2, PyGTK 2.13.0 (Gtk+ 2.14.7)). Maybe Windows 
overrides the change. Note the color change isn't visible when the 
cursor is over the button because it is in the prelight state.

John


More information about the pygtk mailing list