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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access