gtkSwitch in pygtk 2 on windows
Question: I'm creating a python program to switch some hardware devices on and off. The
program has to run on windows. I would really like to use the [gtkSwitch
widget](http://developer.gnome.org/gtk3/3.0/GtkSwitch.html) that was created
in gtk 3.0, because then it is immediately obvious that you want a device to
be switched on or off, but unfortunately gtk3 has not been properly ported to
windows for python. So is there any way to use the gtkswitch that comes with
gtk 3.0 without having to write the program in gtk3, or does anyone know of a
way to use python bindings for gtk 3.0 on windows?

thanks a lot!

Dirk Boonzajer


Answer: please see my code

<https://gist.github.com/shellexy/2f86cfa4a0448f7125e8>

I use gtk.ToggleButton to simulation gtk.Switch button.

<http://i.stack.imgur.com/fLvq7.png>

gtkswitchbutton.py

    
    
    
        #!/usr/bin/python
        # -*- coding: UTF-8 -*-
        # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
        '''GtkSwitch for PyGtk2
        @author: Shellexy 
        @license: LGPLv3+
        @see: 
        '''
    
        import gtk, gobject
    
        try: import i18n
        except: from gettext import gettext as _
    
    
        class SwitchButton(gtk.Table):
            __gtype_name__ = 'SwitchButton'
            __gsignals__ = {
                'toggled': (gobject.SIGNAL_RUN_LAST, None, ()),
            }
            def __init__(self):
                gtk.Table.__init__(self)
                self.label = _("ON\tOFF")
                self.tbutton = gtk.ToggleButton()
                self.tbutton.set_label(self.label)
                self.cbutton = gtk.Button()
                self.cbutton.set_sensitive(True)
                self.cbutton.set_can_focus(False)
                self.cbutton.set_can_default(False)
                self.attach(self.cbutton, 0, 2, 0, 1)
                self.attach(self.tbutton, 0, 4, 0, 1)
                self.tbutton.connect('toggled', self.do_switch)
                self.cbutton.connect('clicked', lambda *args: self.clicked)
                #self.connect('realize', self.do_switch)
                self.set_active(False)
                self.show_all()
                pass
    
            def toggled(self, *args):
                return self.clicked()
    
            def clicked(self, *args):
                return self.tbutton.clicked()
    
            def set_inconsistent(self, setting):
                return self.tbutton(setting)
    
            def set_active(self, is_active):
                return gobject.idle_add(self.tbutton.set_active, not is_active)
    
            def get_active(self):
                return not self.tbutton.get_active()
    
            def do_switch(self, *args):
                return gobject.idle_add(self._do_switch, *args)
    
            def _do_switch(self, *args):
                t = self
                tb = self.tbutton
                b = self.cbutton
                l = tb.get_child()
                l.set_justify(gtk.JUSTIFY_FILL)
                bs = tb.get_style().copy()
                ls = l.get_style().copy()
                bs.bg[gtk.STATE_NORMAL] = ls.bg[gtk.STATE_SELECTED]
                ls.fg[gtk.STATE_NORMAL] = ls.text[gtk.STATE_SELECTED]       
                if self.get_children():
                    t.remove(b); t.remove(tb)
                    pass
                if self.get_active():
                    t.attach(b, 2, 4, 0, 1) ; t.attach(tb, 0, 4, 0, 1)
                    bs.bg[gtk.STATE_PRELIGHT] = ls.bg[gtk.STATE_SELECTED]
                    ls.fg[gtk.STATE_PRELIGHT] = ls.text[gtk.STATE_SELECTED]
                    pass
                else:
                    t.attach(b, 0, 2, 0, 1) ; t.attach(tb, 0, 4, 0, 1)
                    bs.bg[gtk.STATE_PRELIGHT] = ls.bg[gtk.STATE_INSENSITIVE]
                    ls.fg[gtk.STATE_PRELIGHT] = ls.text[gtk.STATE_NORMAL]
                    pass
                tb.set_style(bs)
                l.set_style(ls)
                tb.do_focus(tb, 1)
                self.emit('toggled')
                pass
    
    
        def main():
            window = gtk.Window()
            window.set_title('PyGtk2 SwitchButton')
            options = [
                    ['Connect: ', False],
                    ['Auto Connect: ', True],
                    ['Auto Proxy: ', True],
            ]
            vbox = gtk.VBox()
            vbox.set_spacing(5)
            def foo(*args):
                print args
            for k, v in options:
                s = SwitchButton()
                s.set_active(v)
                s.connect('toggled', foo, k)
                hbox = gtk.HBox()
                label = gtk.Label(k)
                label.set_alignment(0, 0.5)
                hbox.pack_start(label, 1, 1, 0)
                hbox.pack_start(s, 0, 0, 0)
                vbox.pack_start(hbox, 0, 0, 0)
                pass
            window.add(vbox)
            window.show_all()
            window.connect('destroy', lambda *args: gtk.main_quit())
            gtk.main()
            pass
    
        if __name__=="__main__":
            main()
    
    

