The ::activate signal on Gtk.Switch is an action signal and emitting it causes the switch to animate.
state-set
The ::state-set signal on Gtk.Switch is emitted to change the underlying state.
例子
代码:
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/14# section 121
TITLE = "Switch"
DESCRIPTION = """
Gtk.Switch is a widget that has two states: on or off.
The user can control which state should be active by clicking the empty area, or by dragging the handle.
"""import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
classSwitcherWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="Switch Demo")
self.set_border_width(10)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
switch = Gtk.Switch()
switch.connect("notify::active", self.on_switch_activated)
switch.set_active(False)
hbox.pack_start(switch, True, True, 0)
switch = Gtk.Switch()
switch.connect("notify::active", self.on_switch_activated)
switch.set_active(True)
hbox.pack_start(switch, True, True, 0)
@staticmethoddefon_switch_activated(switch, gparam):if switch.get_active():
state = "on"else:
state = "off"
print("Switch was turned", state)
defmain():
win = SwitcherWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()