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

Book HomeMastering Perl/TkSearch this book

4.10. Disabling a Button

Typically, Buttons are meant to be pressed. But sometimes you display a Button that you want to be disabled. For example, you may not want users to submit a form unless they've filled out some required fields.

To disable a Button, use the -state option.

-state => "normal" | "disabled" | "active"

The "normal" state is what we've been describing throughout this chapter so far: the Button changes colors when the mouse passes over it and performs the assigned callback (or changes indicator status) when clicked. The "active" state is when the mouse cursor is physically over the Button and is used internally by Perl/Tk. The "disabled" state is when the Button appears grayed out (or with whatever colors have been specified by -disabledforeground and -disabledbackground) and will not respond to the mouse at all.

For example, suppose we have a Button that disables another when it is pressed. The code would look like this:

my $exit_b = $mw->Button(-text => 'Exit', 
                         -command => sub { exit })->pack;
$var = "Disable Exit";
$mw->Button(-textvariable => \$var, 
            -command => sub { my $state = $exit_b->configure(-state);
                              if ($state eq "disabled") {
                                $exit_b->configure(-state => 'normal');
                                $var = "Disable Exit";
                              } else {
                                $exit_b->configure(-state => 'disabled');
                                $var = "Enable Exit";

In this example, a reference to the Exit Button is saved because it needs to be used later to change the state of the Button. Also, note that $exit_b is used inside the scope of the anonymous subroutine. This will work only if $exit_b is left in the global scope of the entire program, so $exit_b will be defined when the anonymous subroutine is executed. Be careful not to set $exit_b to something else; if you do, the anonymous subroutine will refer to the new value in $exit_b when it is invoked, not the value you wanted.

Figure 4-15 shows the window after we have clicked the Disable Exit Button once.

Figure 4-15

Figure 4-15. Window with disabled Button (Exit) and normal Button

A Button should not be available for selecting unless it makes sense in the application. By disabling widgets when they can't do anything, you give users visual hints about what they can and cannot do in the application.

Sometimes the terminology becomes confusing when talking about state and status. With Checkbuttons and Radiobuttons, there is the indicator's status (or value) and the state of the Button itself. The status of the button is either on or off, depending on whether it is checked. The state of the entire Checkbutton (including the indicator) can be normal, active, or disabled.

Library Navigation Links

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