Gtk.RadioButton


Gtk.RadioButton — A choice from multiple check buttons

Object Hierarchy:

    GObject
    ╰── GInitiallyUnowned
        ╰── Gtk.Widget
            ╰── Gtk.Container
                ╰── Gtk.Bin
                    ╰── Gtk.Button
                        ╰── Gtk.ToggleButton
                            ╰── Gtk.CheckButton
                                ╰── Gtk.RadioButton

See also:

Gtk.ComboBox


Functions:

Description:

A single radio button performs the same basic function as a Gtk.CheckButton, as its position in the object hierarchy reflects. It is only when multiple radio buttons are grouped together that they become a different user interface component in their own right.

Every radio button is a member of some group of radio buttons. When one is selected, all other radio buttons in the same group are deselected. A Gtk.RadioButton is one way of giving the user a choice from many options.

Radio button widgets are created with Gtk.RadioButton::new(), passing None as the argument if this is the first radio button in a group. In subsequent calls, the group you wish to add this button to should be passed as an argument. Optionally, Gtk.RadioButton::new_with_label() can be used if you want a text label on the radio button.

Alternatively, when adding widgets to an existing group of radio buttons, use Gtk.RadioButton::new_from_widget() with a Gtk.RadioButton that already has a group assigned to it. The convenience function Gtk.RadioButton::new_with_label_from_widget() is also provided.

To retrieve the group a Gtk.RadioButton is assigned to, use Gtk.RadioButton::get_group().

To remove a Gtk.RadioButton from one group and make it part of a new one, use Gtk.RadioButton::set_group().

The group list does not need to be freed, as each Gtk.RadioButton will remove itself and its list item when it is destroyed.

How to create a group of two radio buttons.
        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)

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

When an unselected button in the group is clicked the clicked button receives the Gtk.ToggleButton::toggled signal, as does the previously selected button.

Inside the Gtk.ToggleButton::toggled handler, Gtk.ToggleButton::get_active() can be used to determine if the button has been selected or deselected.


Function Details:

new()

new (group:list=None) -> Gtk.Widget

Creates a new Gtk.RadioButton. To be of any practical value, a widget shouldthen be packed into the radio button.

  • Returns: a new radio button

new_from_widget()

new_from_widget (radio_group_member:Gtk.RadioButton=None) -> Gtk.Widget

Creates a new Gtk.RadioButton, adding it to the same group asradio_group_member. As with Gtk.RadioButton:new(), a widgetshould be packed into the radio button. [constructor]

  • Returns: a new radio button.

new_with_label()

new_with_label (group:list=None, label:str) -> Gtk.Widget

Creates a new Gtk.RadioButton with a text label.

  • Returns: a new radio button.

new_with_label_from_widget()

new_with_label_from_widget (radio_group_member:Gtk.RadioButton=None, label:str) -> Gtk.Widget

Creates a new Gtk.RadioButton with a text label, adding it tothe same group as radio_group_member. [constructor]

  • Returns: a new radio button.

new_with_mnemonic()

new_with_mnemonic (group:list=None, label:str) -> Gtk.Widget

Creates a new Gtk.RadioButton containing a label, adding it to the samegroup as group. The label will be created using`Gtk.Label:new_with_mnemonic(), so underscores inlabel` indicate themnemonic for the button.

  • Returns: a new Gtk.RadioButton

new_with_mnemonic_from_widget()

new_with_mnemonic_from_widget (radio_group_member:Gtk.RadioButton=None, label:str) -> Gtk.Widget

Creates a new Gtk.RadioButton containing a label. The labelwill be created using `Gtk.Label:new_with_mnemonic(), so underscoresinlabel` indicate the mnemonic for the button. [constructor]

  • Returns: a new Gtk.RadioButton.

set_group()

set_group (self, group:list=None)

Sets a Gtk.RadioButton’s group. It should be noted that this does not changethe layout of your interface in any way, so if you are changing the group,it is likely you will need to re-arrange the user interface to reflect thesechanges.


get_group()

get_group (self) -> list

Retrieves the group assigned to a radio button.

  • Returns: a linked listcontaining all the radio buttons in the same groupas radio_button. The returned list is owned by the radio buttonand must not be modified or freed.

join_group()

join_group (self, group_source:Gtk.RadioButton=None)

Joins a Gtk.RadioButton object to the group of another Gtk.RadioButton object Use this in language bindings instead of the Gtk.RadioButton:get_group() and Gtk.RadioButton:set_group() methods A common way to set up a group of radio buttons is the following: Gtk.RadioButton radio_button; Gtk.RadioButton last_button; while ( ...more buttons to add... ) { radio_button = Gtk.RadioButton:new (...); Gtk.RadioButton:join_group (radio_button, last_button); last_button = radio_button; }

  • Since: 3.0

Example:

import gi
gi.require_version('Gtk', '3.0')
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()

results matching ""

    No results matching ""