my $rb = $mw->Radiobutton( [ option => value ] );with some basic options being
#!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.
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.
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