4. Radio Buttons

Radio buttons are similar to check buttons except they are grouped so that only one may be selected/depressed at a time. This is good for places in your application where you need to select from a short list of options.

Creating a new radio button is done with one of these calls:

$radiobutton = Gtk2::RadioButton->new($member_or_listref=undef, $label=undef);

$radiobutton = Gtk2::RadioButton->new_from_widget($group, $label=undef);

$radiobutton = Gtk2::RadioButton->new_with_label($member_or_listref=undef, $label=undef);

$radiobutton = Gtk2::RadioButton->new_with_label_from_widget($group, $label=undef);

$radiobutton = Gtk2::RadioButton->new_with_mnemonic($member_or_listref=undef, $label=undef);

$radiobutton = Gtk2::RadioButton->new_with_mnemonic_from_widget($group, $label=undef);

You'll notice the extra argument to these calls. They require a group to perform their duty properly. The first call to Gtk2::RadioButton::new() or Gtk2::RadioButton::new_with_label() should pass undef as the first argument. Then create a group using:


The important thing to remember is that Gtk2::RadioButton::get_group() must be called for each new button added to the group, with the previous button passed in as an argument. The result is then passed into the next call to Gtk2::RadioButton::new() or Gtk2::RadioButton::new_with_label(). This allows a chain of buttons to be established. The example below should make this clear.

You can shorten this slightly by using the following syntax, which removes the need for a variable to hold the list of buttons:

$radiobutton = Gtk2::RadioButton->new_with_label(

The _from_widget() variants of the creation functions allow you to shorten this further, by omitting the Gtk2::RadioButton::get_group() call. This form is used in the example to create the third button:

$radiobutton = Gtk2::RadioButton->new_with_label_from_widget(

It is also a good idea to explicitly set which button should be the default depressed button with:

Gtk2::ToggleButton->set_active($toggle_button, gboolean state);

This is described in the section on toggle buttons, and works in exactly the same way. Once the radio buttons are grouped together, only one of the group may be active at a time. If the user clicks on one radio button, and then on another, the first radio button will first emit a "toggled" signal (to report becoming inactive), and then the second will emit its "toggled" signal (to report becoming active).

The following example creates a radio button group with three buttons.

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

$window = Gtk2::Window->new('toplevel');
$window->signal_connect(delete_event => sub { Gtk2->main_quit; return FALSE; });
$window->set_title("radio buttons");

$box1 = Gtk2::VBox->new(FALSE, 0);

$box2 = Gtk2::VBox->new(FALSE, 10);
$box1->pack_start($box2, TRUE, TRUE, 0);

$button = Gtk2::RadioButton->new(undef, "button 1");
$box2->pack_start($button, TRUE, TRUE, 0);

@group = $button->get_group;
$button = Gtk2::RadioButton->new_with_label(@group, "button 2");
$box2->pack_start($button, TRUE, TRUE, 0);

$button = Gtk2::RadioButton->new_with_label_from_widget($button, "button 3");
$box2->pack_start($button, TRUE, TRUE, 0);

$separator = Gtk2::HSeparator->new;
$box1->pack_start($separator, FALSE, TRUE, 0);

$box2 = Gtk2::VBox->new(FALSE, 10);
$box1->pack_start($box2, FALSE, TRUE, 0);

$button = Gtk2::Button->new("close");
$button->signal_connect(clicked => sub { Gtk2->main_quit; });
$box2->pack_start($button, TRUE, TRUE, 0);