Radiobuttons

A radiobutton widget is like a checkbutton, but is used when there are more than two choices available and/or the choices are not (conveniently) phraseable as the answer to a ``yes/no'' question. Are you female or male? Which of the following age groups do you fall under? The basic syntax is
my $rb = $mw->Radiobutton( [ option => value ] );
with some basic options being We give two examples similar to those of the checkbutton. The first
#!perl
# file rb.pl
use Tk;
use strict;
use warnings;
my $rb_value = 'red';
my $message = "Your favourite colour is $rb_value";
my $mw = MainWindow->new;
$mw->title('Radiobutton');
my $label = $mw->Label(-textvariable => \$message);
my $red = $mw->Radiobutton(-text => 'Red',
                           -variable => \$rb_value,
                           -value => 'red');
my $green = $mw->Radiobutton(-text => 'Green',
                             -variable => \$rb_value,
                             -value => 'green');
my $blue = $mw->Radiobutton(-text => 'Blue',
                            -variable => \$rb_value,
                            -value => 'blue');
my $show = $mw->Button(-text => 'Show favourite',
                       -command => \&display);
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$label->pack;
$red->pack;
$green->pack;
$blue->pack;
$show->pack;
$exit->pack;
MainLoop;
sub display {
  $message = "Your favourite colour is $rb_value";
}
is illustrated in the figure below.

Figure 3.8: Example of a radiobutton widget
Image rb

In this example three radiobuttons - $red, $green, and $blue - are used, each associated with the variable $rb_value, the value of which indicates the colour selected. The $show button, when clicked, will update the $message string using the current value of $rb_value, which will subsequently be reflected in the label displayed by $label.

The second example,

#!perl
# file rb1.pl
use Tk;
use strict;
use warnings;
my $rb_value = 'red';
my $message = "Your favourite colour is $rb_value";
my $mw = MainWindow->new;
$mw->title('Radiobutton');
my $label = $mw->Label(-textvariable => \$message);
my $red = $mw->Radiobutton(-text => 'Red',
                           -variable => \$rb_value,
                           -value => 'red',
                           -command => \&display);
my $green = $mw->Radiobutton(-text => 'Green',
                             -variable => \$rb_value,
                             -value => 'green',
                             -command => \&display);
my $blue = $mw->Radiobutton(-text => 'Blue',
                            -variable => \$rb_value,
                            -value => 'blue',
                            -command => \&display);
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$label->pack;
$red->pack;
$green->pack;
$blue->pack;
$exit->pack;
MainLoop;
sub display {
  $message = "Your favourite colour is $rb_value";
}
is pictured below.

Figure 3.9: Another example of a radiobutton widget
Image rb1

This example is similar to the first, but the code to update the $message string with the current value of $rb_value is accessed in the radiobutton itself through the -command option and the associated display subroutine. In this case the label associated with $label will change as soon as a different radiobutton is selected.

Randy Kobes 2003-11-17