Skip to content

Instantly share code, notes, and snippets.

@shellexy
Last active August 29, 2015 14:03
Show Gist options
  • Save shellexy/2f86cfa4a0448f7125e8 to your computer and use it in GitHub Desktop.
Save shellexy/2f86cfa4a0448f7125e8 to your computer and use it in GitHub Desktop.
GtkSwitch for PyGtk2
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
'''GtkSwitch for PyGtk2
@author: Shellexy <[email protected]>
@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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment