|
|
|
|
|
When you use bind to invoke a callback on an entire widget class, it makes the job of determining which widget was the subject of the event much easier: |
|
|
|
 |
|
|
|
|
$mw->Scrolled("Text")->pack(-expand => 1, -fill => 'both');
$mw->Scrolled("Text")->pack(-expand => 1, -fill =>'both');
$menu = $mw->Menu (-menuitems =>[ ["command" => "Save",
-command => \&save_file],
["command" => "Open",
-command => \&open_file]
],
-tearoff => 0);
$mw->bind(Tk::Text, "<Button-3>",
sub {$menu->Popup(-popover => 'cursor') });
sub save_file {
my ($text) = @_;
open(FH, ">outfile") || die "Couldn't open outfile for writing";
print FH $text->get("1.0", "end");
close (FH);
} |
|
|
|
|
|
|
|
|
The call to bind uses Tk: :Text as the first argument. This will cause the bind to be applied to every text widget in the application. In this example, no matter which text widget is clicked, its contents will be written to "outfile". The application might also prompt the user for a different filename at that point, allowing it to actually do something useful. |
|
|
|
|
|
|
|
|
So far, you've seen several different event sequences-<Button-3>, <Button-1>, and <Return>-but I haven't yet explained the format for building them. Although the examples you've seen may seem obvious and simple, event sequences can get much more complicated. |
|
|
|
|
|
|
|
|
The event sequence is built from an optional modifier, an event, and an optional detail. They are separated by dashes and then placed between angle brackets: |
|
|
|
 |
|
|
|
|
<modifier-event-detail> |
|
|
|
|
|
|
|
|
As we discuss all the possible bindings, keep in mind that it is possible for more than one event sequence to match. The more detailed matches will invoke their callbacks first. If a binding has been created on a specific button, and then another binding is created on all of the buttons, the specific-button bind callback will be invoked first, and then the more general all-button bind callback will be invoked. |
|
|
|
|
|
|
|
|
A modifier is an event that happens at the same time the main event happens, such as holding down the Control key and clicking the mouse. The modifying |
|
|
|
|