RadioButton

RadioButton widgets are similar to CheckButton widgets, however they allow only one of a group of RadioButton widgets to be selected at any one time. When another RadioButton in the group is selected, the active state of the button is switched to that and removed from the previous one which was chosen.

A RadioButton is based on the ToggleButton widget, and inherits many of the same methods, properties and signals.

Constructor

The RadioButton can be constructed using the following:

radiobutton = Gtk.RadioButton(label, group, use_underline)

The label value should be set to describe the function of the RadioButton. The group parameter is set to the name of the first RadioButton to be included in the group. For the first RadioButton, this should be set to None. Subsequent RadioButton widgets should then have their group parameter set to the name of the first RadioButton. When use_underline is set to True, the character preceded by an underscore will be marked as the accelerator character.

Methods

To join a RadioButton to a group after construction use:

radiobutton.join_group(group)

The label associated with the RadioButton can be set after constructing with:

radiobutton.set_label(label)

It is good practice to use a mnemonic in the label. This requires an underscore inserted into the label (e.g. “_Cancel”). GTK+ parses the underscore and converts it into an underline beneath the following character, which the user can then access as a shortcut to the function.

radiobutton.set_use_underline(use_underline)

To set the RadioButton as active call the method:

radiobutton.set_active(active)

When active is set to True, the RadioButton indicator will feature a dot.

The active state of the RadioButton can also be retrieved using:

radiobutton.get_active()

In some cases, the RadioButton may be set to an inconsistent state, which is used to indicate the status of other RadioButton widgets. For example, three RadioButton’s may be a mix of set and unset, which leaves the fourth set as inconsistent. This can be set programatically with:

radiobutton.set_inconsistent(inconsistent)

To retrieve whether a RadioButton is set as inconsistent use:

radiobutton.get_inconsistent()

If the RadioButton is in an inconsistent state, True will be returned.

Signals

The commonly used signals of a RadioButton are:

"toggled" (radiobutton)
"group-changed" (radiobutton)

A "toggled" signal emits from the RadioButton when the mode is changed to active or inactive. When using this signal, you will need to check which RadioButton is receiving the active or inactive state. This is down to the "toggled" signal being emitted twice; once for the RadioButton becoming active and again for the RadioButton becoming inactive. The "group-changed" signal emits whenever a RadioButton changes which group it belongs to.

Example

Below is an example of a RadioButton:

#!/usr/bin/env python3

from gi.repository import Gtk

class RadioButton(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        box = Gtk.Box()
        box.set_orientation(Gtk.Orientation.VERTICAL)
        box.set_spacing(5)
        self.add(box)

        radiobutton1 = Gtk.RadioButton(label="RadioButton 1")
        radiobutton1.connect("toggled", self.on_radio_button_toggled)
        box.pack_start(radiobutton1, True, True, 0)
        radiobutton2 = Gtk.RadioButton(label="RadioButton 2", group=radiobutton1)
        radiobutton2.connect("toggled", self.on_radio_button_toggled)
        box.pack_start(radiobutton2, True, True, 0)
        radiobutton3 = Gtk.RadioButton(label="RadioButton 3", group=radiobutton1)
        radiobutton3.connect("toggled", self.on_radio_button_toggled)
        box.pack_start(radiobutton3, True, True, 0)

    def on_radio_button_toggled(self, radiobutton):
        if radiobutton.get_active():
            print("%s is active" % (radiobutton.get_label()))

window = RadioButton()
window.show_all()

Gtk.main()

Download: RadioButton