Debian Codegraph @

docs/tutorials/gtk4/controls/examples/togglebutton.py

raw
1 import gi
2
3 gi.require_version("Gtk", "4.0")
4 from gi.repository import Gtk
5
6
7 class ToggleButtonWindow(Gtk.ApplicationWindow):
8 def __init__(self, **kargs):
9 super().__init__(**kargs, title="ToggleButton Demo")
10
11 hbox = Gtk.Box(spacing=6)
12 self.set_child(hbox)
13
14 button = Gtk.ToggleButton(label="Button 1")
15 button.connect("toggled", self.on_button_toggled, "1")
16 hbox.append(button)
17
18 button = Gtk.ToggleButton(label="B_utton 2", use_underline=True)
19 button.set_active(True)
20 button.connect("toggled", self.on_button_toggled, "2")
21 hbox.append(button)
22
23 def on_button_toggled(self, button, name):
24 state = "on" if button.props.active else "off"
25 print("Button", name, "was turned", state)
26
27
28 def on_activate(app):
29 win = ToggleButtonWindow(application=app)
30 win.present()
31
32
33 app = Gtk.Application(application_id="com.example.App")
34 app.connect("activate", on_activate)
35
36 app.run(None)