在Windows上的pygtk 2中的gtkSwitch

时间:2012-12-01 22:44:51

标签: python pygtk gtk3 gtk2

我正在创建一个python程序来打开和关闭一些硬件设备。该程序必须在Windows上运行。我真的想使用在gtk 3.0中创建的gtkSwitch widget,因为很明显你想要打开或关闭设备,但遗憾的是gtk3还没有正确移植到windows for python。那么有没有办法使用gtk 3.0附带的gtkswitch而不必在gtk3中编写程序,或者有没有人知道在Windows上使用gtk 3.0的python绑定的方法?

非常感谢!

Dirk Boonzajer

1 个答案:

答案 0 :(得分:-1)

请参阅我的代码

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

我使用gtk.ToggleButton来模拟gtk.Switch按钮。

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