Standard Options for Each Button Type

Before we get into all the options available for each of the Button widgets, let's take a look at the most common ones.

When creating a Button, use the -text and -command options. The -text option lets the user know what the Button is for, and the -command option makes something happen when the user clicks the Button.

$b = $mw->Button(-text => 'Exit', -command => sub {
 exit;
}
)->pack; # Use the same sub for many Buttons $b = $mw->Button(-text => 'Red', -command => [\&change_color, 'red'])->pack; $b = $mw->Button(-text => 'Blue', -command => [\&change_color, 'blue'])->pack; $b = $mw->Button(-text => 'Green', -command => [\&change_color, 'green'])->pack;

When creating Checkbuttons, you use -variable in addition to -text. Using -variable gives you an easy way to find out whether the Checkbutton is checked. (You will rarely use -command with a Checkbutton):

$mw->Checkbutton(-text => 'Print Header', -variable => \$print_header); sub print_document {
 if ($print_header) {
 # Code to print header here...
}
}

The value stored in $print_header is 1 or 0. A simple test will tell you if the Checkbutton was checked.

When creating Radiobuttons, we always create more than one and use the -text, -variable, and -value options:

$group1 = 100; # set default value foreach (qw/1 10 100 10000 100000 1000000/) {
 $mw->Radiobutton(-text => '$' . $_, -variable => \$group1, -value => $_)->pack(-side => 'left');
}
print "User selected: $group1";

The variable $group1 relates all of the Radiobuttons, making it so the user can select only one at a time. Each Radiobutton must be given a -value to store in $group1 (there is no default).