Check & Radio Buttons — PyGObject

Skip to main content
`Ctrl`+`K`

# 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

     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        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
    41def on_activate(app):
    42    win = CheckButtonWindow(application=app)
    43    win.present()
    44
    45
    46app = Gtk.Application(application_id="com.example.App")
    47app.connect("activate", on_activate)
    48
    49app.run(None)