Object
+--- Widget
+--- Container
+--- Bin
+--- Button
+--- ToggleButton
+--- CheckButton
+--- RadioButton
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:
new Gtk::RadioButton( $label );
new Gtk::RadioButton( $label, $button );
# create the first button and add it to a box
You'll notice the extra argument to the second call. Radio
buttons require a button group to perform their duty
properly. The first call to
new Gtk::RadioButton()
should have the label as its only argument. Then you add other
radio buttons to the group by using one of the buttons already
in the group as the second argument. This code snippet should
make this a little clearer:
$radio1 = new Gtk::RadioButton( "button 1" );
$box->add( $radio1 );
# create the second button and add it to a box
$radio2 = new Gtk::RadioButton( "button 2", $radio1 );
$box->add( $radio2 );
# create the third button and add it to a box
# note that the first button created is the second argument
$radio3 = new Gtk::RadioButton( "button 3", $radio1 );
$box->add( $radio3 );
# create the fourth button and add it to a box
# note that the third button created is the second argument
$radio4 = new Gtk::RadioButton( "button 4", $radio3 );
$box->add( $radio4 );
It is also a good idea to explicitly set which button should be
the default depressed button with:
$togglebutton->set_active( $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).
If you want to get a list of all the radio button in a group, you
can use the following function:
@group = $radiobutton->button_group();
If you want to add a RadioButton to a button group after it has
been created, you may do so with:
$radiobutton->button_set_group( @group );
The
@group
argument is a list of radio buttons, all of which are in the
same button group. This is usually obtained from the
button_group()
function.
The following example creates a radio button group with four buttons.
#!/usr/bin/perl -w use Gtk; use strict; set_locale Gtk; init Gtk; my $false = 0; my $true = 1; my $window; my $box1; my $box2; my $button; my $separator; # Create the window $window = new Gtk::Window( "toplevel" ); $window->signal_connect( "delete_event", sub { Gtk->exit( 0 ); } ); $window->set_title( "Radio Buttons" ); $window->border_width( 0 ); $box1 = new Gtk::VBox( $false, 0 ); $box1->show(); $box2 = new Gtk::VBox( $false, 10 ); $box2->border_width( 10 ); $box1->pack_start( $box2, $false, $false, 0 ); $box2->show(); $window->add( $box1 ); # Create the radio buttons $button = new Gtk::RadioButton( "button 1" ); $box2->pack_start( $button, $false, $false, 0 ); $button->show(); $button = new Gtk::RadioButton( "button 2", $button ); $button->set_active( $true ); $box2->pack_start( $button, $true, $true, 0 ); $button->show(); $button = new Gtk::RadioButton( "button 3", $button ); $box2->pack_start( $button, $true, $true, 0 ); $button->show(); $button = new Gtk::RadioButton( "button 4", $button ); $box2->pack_start( $button, $true, $true, 0 ); $button->show(); $separator = new Gtk::HSeparator(); $box1->pack_start( $separator, $false, $false, 0 ); $separator->show(); $box2 = new Gtk::VBox( $false, 10 ); $box2->border_width( 10 ); $box1->pack_start( $box2, $false, $true, 0 ); $box2->show(); # Create the close button $button = new Gtk::Button( "Close" ); $button->signal_connect( "clicked", sub { Gtk->exit( 0 ); } ); $box2->pack_start( $button, $true, $true, 0 ); $button->can_default( $true ); $button->grab_default(); $button->show(); $window->show(); main Gtk; exit( 0 ); # END EXAMPLE PROGRAM
Once you run this program you should see something like this (notice that the default radio button is the second one):

RadioButton Example Screenshot