| 1 |
import gi |
| 2 |
|
| 3 |
gi.require_version("Gtk", "4.0") |
| 4 |
from gi.repository import Gtk |
| 5 |
|
| 6 |
|
| 7 |
class CheckButtonWindow(Gtk.ApplicationWindow): |
| 8 |
def __init__(self, **kargs): |
| 9 |
super().__init__(**kargs, title="CheckButton Demo") |
| 10 |
|
| 11 |
box = Gtk.Box(spacing=6) |
| 12 |
self.set_child(box) |
| 13 |
|
| 14 |
check = Gtk.CheckButton(label="Checkbox") |
| 15 |
check.connect("toggled", self.on_check_toggled) |
| 16 |
box.append(check) |
| 17 |
|
| 18 |
radio1 = Gtk.CheckButton(label="Radio 1") |
| 19 |
radio1.connect("toggled", self.on_radio_toggled, "1") |
| 20 |
box.append(radio1) |
| 21 |
|
| 22 |
radio2 = Gtk.CheckButton(label="Radio 2") |
| 23 |
radio2.set_group(radio1) |
| 24 |
radio2.connect("toggled", self.on_radio_toggled, "2") |
| 25 |
box.append(radio2) |
| 26 |
|
| 27 |
radio3 = Gtk.CheckButton.new_with_mnemonic("R_adio 3") |
| 28 |
radio3.set_group(radio1) |
| 29 |
radio3.connect("toggled", self.on_radio_toggled, "3") |
| 30 |
box.append(radio3) |
| 31 |
|
| 32 |
def on_check_toggled(self, check): |
| 33 |
state = "on" if check.props.active else "off" |
| 34 |
print("Checkbox was turned", state) |
| 35 |
|
| 36 |
def on_radio_toggled(self, radio, name): |
| 37 |
state = "on" if radio.props.active else "off" |
| 38 |
print("Radio", name, "was turned", state) |
| 39 |
|
| 40 |
|
| 41 |
def on_activate(app): |
| 42 |
win = CheckButtonWindow(application=app) |
| 43 |
win.present() |
| 44 |
|
| 45 |
|
| 46 |
app = Gtk.Application(application_id="com.example.App") |
| 47 |
app.connect("activate", on_activate) |
| 48 |
|
| 49 |
app.run(None) |