Radiobutton Values
Groups of Radiobuttons are designed to all
use the same -variable option but with unique
-value options for each individual Radiobutton.
When you select a Radiobutton, any other Radiobuttons assigned the
same -variable will be unselected, and that
variable will be assigned the -value associated
with the selected Button.
You must always use -value when creating
Radiobuttons. If you don’t, they won’t select/deselect
correctly. There are no defaults for -value.
Here is an example that uses Radiobuttons to control the background color:
# setup the default value we would like
$rb_value = "red";
$mw->configure(-background => $rb_value);
# create the Radiobuttons that will let us change it
foreach (qw(red yellow green blue grey)) {
$mw->Radiobutton(-text => $_,
-value => $_,
-variable => \$rb_value,
-command => \&set_bg)->pack(-side => 'left');
}
# function to change the background color using $rb_value
sub set_bg {
print "Background value is now: $rb_value\n";
$mw->configure(-background => $rb_value);
}We are storing the status of our Radiobutton group in
$rb_value. We set it to an initial value of
"red", which happens to match the first
Radiobutton we are creating. When any of the Radiobuttons are
clicked, including the one currently selected, the subroutine
set_bg will be called. This subroutine will print
the new value of $rb_value and then change the
background of our MainWindow to that color.
If you look at the code closely, you see that we call the
configure ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access