[pygtk] Color a ToggleButton

E.R. Uber eruber at gmail.com
Sun Sep 6 06:39:58 WST 2009


After reading PyGTK FAQ 4.6 at
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 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.

Here is my code:

#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk


class demoApp(object):

    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file("toggle.glade")

        self.window   = builder.get_object("window")

        self.eventbox = builder.get_object("eventbox")
        self.toggle   = builder.get_object("togglebutton")

        builder.connect_signals(self)

    def on_togglebutton_toggled(self, widget, data=None):
        map   = widget.get_colormap()
        color_off = map.alloc_color("red")
        color_on  = map.alloc_color("green")

        label = widget.get_label()
        if label == '1':
            widget.set_label('0')

            # FAQ 4.6
            #style = widget.get_style().copy()
            #style.bg[gtk.STATE_NORMAL] = color_off
            #widget.set_style(style)

            eventbox = widget.get_parent()
            eventbox.modify_bg(gtk.STATE_NORMAL, color_off)
        else:
            widget.set_label('1')

            # FAQ 4.6
            #style = widget.get_style().copy()
            #style.bg[gtk.STATE_NORMAL] = color_on
            #widget.set_style(style)

            eventbox = widget.get_parent()
            eventbox.modify_bg(gtk.STATE_NORMAL, color_on)

    def on_window_destroy(self, widget, data=None):
        gtk.main_quit()

    def main(self):
        self.window.show()
        gtk.main()

if __name__ == "__main__":
    app = demoApp()
    app.main()

# toggle.glade file:
<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window">
    <signal name="destroy" handler="on_window_destroy"/>
    <child>
      <object class="GtkTable" id="table1">
        <property name="visible">True</property>
        <property name="n_rows">3</property>
        <property name="n_columns">3</property>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <object class="GtkEventBox" id="eventbox">
            <property name="visible">True</property>
            <child>
              <object class="GtkToggleButton" id="togglebutton">
                <property name="label" translatable="yes">1</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="toggled" handler="on_togglebutton_toggled"/>
              </object>
            </child>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
            <property name="x_options"></property>
            <property name="y_options"></property>
            <property name="x_padding">5</property>
            <property name="y_padding">5</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090905/ca7d5a27/attachment-0001.htm 


More information about the pygtk mailing list