14.1. The bind Method
To use the bindmethod, invoke it from the widget to which you would like to add the binding. For instance, if you want to add a binding to a button in $button, use $button->bind.In certain instances, you would use the main window of your application: $mw->bind(...). There are several different sets of valid arguments you can send to bind. The following list explains them all:
$widget->bind();
Calling bind with no arguments returns a list of bind sequences (e.g. <Button-1>, <Key-D>) that have been created for that widget. It will not return any of the default bindings. Here's an example:
$button = $mw->Button( ... )->pack; $button->bind("<Button-3>", sub { ... } ); ... @bindings = $button->bind(); print "Bindings for button: @bindings\n"; # would print: # Bindings for button: <Button-3>
This function will return an empty string if there are no additional bindings for that widget.
$widget->bind(sequence);
You can determine what callback is associated with a bind sequence. Pass in the bind sequence (for example, "<Button-3>") as the first argument and the currently assigned callback will be returned. Expanding the preceding example, we can use the information in @bindings to see what callbacks are associated with them:
foreach (@bindings) {
print "$_ is assigned callback ", $button->bind($_), "\n";
}
# <Button-3> is assigned callback Tk::Callback=CODE(0x91fdcc)
If you send a bind sequence that doesn't exist for that widget, you'll simply get an empty string ...
Get Learning Perl/Tk now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.