home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeMastering Perl/TkSearch this book

4.6. Checkbutton and Radiobutton Indicator Status

With Checkbutton and Radiobutton widgets, you must include a -variable option to associate a variable with the status of the indicator.

-variable => \$value

How the variable is used, however, varies between Checkbuttons and Radiobuttons. When a Checkbutton is clicked, $value contains the status of the indicator; typically 0 if the indicator is off and 1 if the indicator is on. You can use the -onvalue and -offvalue options to change those defaults (more on that soon).

With Radiobuttons, there is no default value, and you need to explicitly assign $value with the -value option. We'll show some examples of this in Section 4.8, "Radiobutton Values".

By changing the contents of $value, you can change the status of the indicator. Changing the contents of $value will toggle the indicator on a Checkbutton; on a Radiobutton, the currently selected Radiobutton might change. It is important to note that the subroutine associated with -command (if there is one) is not invoked when the value of $value is changed. (See the next section for more information on using the -command option.)

Reading the value associated with -variable is usually the easiest way to check the status of the indicator on the Button. Here is an example with two Buttons that change the $cb_value variable:

$cb_value = 0;
$cb = $mw->Checkbutton(-text => "Checkbutton",
                       -variable => \$cb_value,
                       -command => sub { print "Clicked! $cb_value\n" }
                       )->pack(-side => 'top');

$mw->Button(-text => "CB on",
            -command => sub { $cb_value = 1 })->pack(-side => 'left');
$mw->Button(-text => "CB off",
            -command => sub { $cb_value = 0 })->pack(-side => 'left');

See Figure 4-11 for the resulting window.

Figure 4-11

Figure 4-11. Buttons changing the value of a Checkbutton

The value stored in $cb_value can be changed in three ways: clicking the Checkbutton, clicking the "CB off" Button, or clicking the "CB on" Button. Only when you click on the Checkbutton will you see the word "Clicked!" written in the shell window from which it was run, followed by the value of $cb_value.

There are other ways to change the value associated with the Checkbutton. See invoke, select, deselect, and toggle, later in this chapter.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.