|
|
|
|
|
There are several default bitmaps: error, gray12, gray25, gray50, gray75, hourglass, info, questhead, question, and warning (see Figure 3-3). They are specified in the option by placing single quotes around the bitmap name: |
|
|
|
 |
|
|
|
|
$mw->Button(-bitmap => 'error', -command => \&handle_error)->pack; |
|
|
|
|
|
|
|
|
To specify a bitmap from a file, you need to put an @ in front of the path. |
|
|
|
 |
|
|
|
|
$mw->Button(-bitmap => '@/usr/nwalsh/mybitmap',
-command => sub { exit })->pack; |
|
|
|
|
|
|
|
|
Note that, if you use double quotes, you have to escape the @ with a backslash, (e.g., "\@/usr/nwalsh/mybitmap"). |
|
|
|
|
|
|
|
|
Figure 3-3.
Window showing all the default bitmaps |
|
|
|
|
|
|
|
|
In addition to the -text option, the -command option is almost always used to create a button. For the button to do something when pressed, we have to associate a callback with the button via the -command option. The callback happens when mouse button 1 is released over the button.* If you click down on the button but move the cursor away from the button before releasing, nothing happens because the mouse-click was aborted. |
|
|
|
|
|
|
|
|
In the Hello World program, we used the exit routine as our callback: |
|
|
|
 |
|
|
|
|
$mw->Button(-text => "Done", -command => sub { exit })->pack; |
|
|
|
|
|
|
|
|
There are several ways to associate a subroutine or set of commands with the button. This discussion will apply to all widgets that have a -command option, so you will see this option referred to often. |
|
|
|
|
|
|
|
|
Defining a -command Callback |
|
|
|
|
|
|
|
|
There are several ways the callback can be defined: |
|
|
|
|
|
|
|
|
• Anonymous subroutine: e.g., sub { .. do something .. } |
|
|
|
|
|
|
|
|
• Reference to a subroutine: e.g., \&mysub |
|
|
|
|
|
|
|
|
• Anonymous list with the first element as a subroutine pointer, and the rest of the list as arguments to the subroutine: [ \&mysub, $arg0, $arg1, \@arg2 ...] |
|
|
|
 |
|
 |
|
|
* Mouse button 1 is the leftmost mouse button, mouse button 2 is the middle mouse button, and mouse button 3 is the rightmost mouse button. |
|
|
|
|