Globally, there are two ways to bind an action to an event. The first way is
with the -command
option of a widget, for example, if .b
is a
button
.b configure -command { puts "Button .b is pressed" }will result in the text "Button .b is pressed" printed out every time the button is pressed. The
-command
option is used to bind an action that
is to be invoked when a widget is invoked. Not all widgets support this kind
of binding, for example a listbox can not be `invoked'.
The second way will work with almost all widgets and handles events that are
generated by the X Windows system (or whatever graphical environment you're
running). To bind this kind of events, the bind
command is used,
which has the following syntax:
bind widget-name
<event> script
Suppose we want to bind the event of releasing the middle mouse button when
the mouse cursor is positioned over button .b
to an action that
consists of printing some text. That could be done as follows:
bind .b <Button2-ButtonRelease> { puts "Button 2 released over .b" }
Almost every event that the X Window system generates can be bound to an
action. Among the most useful events are pressing and releasing a mouse
button, moving the mouse cursor, pressing and releasing a key and entering
and leaving a widget with the mouse cursor. See [1] for the
complete syntax of the events and the bind
command.