Check & Radio Buttons¶
A Gtk.CheckButton
places a label next to an indicator.
Check or Radio buttons are created through Gtk.CheckButton
, you create
one or another depending grouping.
Gtk.CheckButton
can be grouped together, to form mutually exclusive
groups so only one of the buttons can be toggled at a time, this create what we
usually refer to as a radio button.
In addition to “on” and “off”, Gtk.CheckButton
can be an “in between”
state that is neither on nor off. This can be used e.g. when the user has
selected a range of elements (such as some text or spreadsheet cells) that are
affected by a check button, and the current values in that range are
inconsistent.
For this purpose use Gtk.CheckButton.props.inconsistent
.
Example¶
![../../../_images/check_radio_buttons.png](../../../_images/check_radio_buttons.png)
1import gi
2
3gi.require_version('Gtk', '4.0')
4from gi.repository import Gtk
5
6
7class 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 if check.props.active:
34 state = 'on'
35 else:
36 state = 'off'
37 print('Checkbox was turned', state)
38
39 def on_radio_toggled(self, radio, name):
40 if radio.props.active:
41 state = 'on'
42 else:
43 state = 'off'
44 print('Radio', name, 'was turned', state)
45
46
47def on_activate(app):
48 win = CheckButtonWindow(application=app)
49 win.present()
50
51
52app = Gtk.Application(application_id='com.example.App')
53app.connect('activate', on_activate)
54
55app.run(None)