beetlebug▾
/

bind

Gives or sets the script that an event of a widget, a class, or another tag runs.

bind TAG
bind TAG SEQUENCE
bind TAG SEQUENCE SCRIPT
bind TAG SEQUENCE +SCRIPT

Description

A binding connects an event sequence of a tag to a script. TAG is the name of a widget, a class such as Button, all, or any other word. Each widget has a list of tags, which bindtags gives: by default the widget, its class, its toplevel, and all. An event of the widget runs the binding of each tag of the list, in order, that has a SEQUENCE that matches.

With TAG only, bind gives the sequences of TAG that have a binding. With SEQUENCE, it gives the script of that binding, or an empty string. With SCRIPT, it sets the binding and gives an empty string. A SCRIPT that starts with + adds its lines to the script of the binding. An empty SCRIPT deletes the binding.

A SEQUENCE is one or more events. Each event is <MODIFIER-MODIFIER-TYPE-DETAIL>, with any count of modifiers and an optional detail. A single character such as a is the key with that character. <<NAME>> is a virtual event of event.

Before the script runs, each % field in it gets a value of the event.

A field that the event does not have gives ??. The script runs at the global level. break in a script stops the bindings of the later tags for this event, and continue stops only the script of this tag. An error of the script goes to bgerror.

Returns

A list of sequences, a script, or an empty string.

Examples

% frame .f -width 20 -height 20
> pack .f
> update
> bind .f <Button-1> {set ::at %W:%x:%y:%b}
> event generate .f <Button-1> -x 3 -y 4
> set at
.f:3:4:1

# `<Key-a>` is shown in its short form `a`.
% bind .f <Key-a> {set ::key a}
> bind .f
a <Button-1>

# `<1>` and `<Button-1>` are the same sequence.
% bind .f <1>
set ::at %W:%x:%y:%b

% bind .f <Key-a> {+set ::again 1}
> bind .f <Key-a>
set ::key a
set ::again 1

% bind .f <Key-a> {}
> bind .f
<Button-1>

% frame .g -width 20 -height 20
> pack .g
> update
> set ::o {}
> bind .g <Button-1> {lappend ::o widget}
> bind Frame <Button-1> {lappend ::o class}
> bind all <Button-1> {lappend ::o all}
> event generate .g <Button-1>
> set o
widget class all

# `break` stops the bindings of the class and of `all`.
% set ::o {}
> bind .g <Button-1> {lappend ::o widget; break}
> event generate .g <Button-1>
> set o
widget

% bind .g <<Saved>> {set ::data %d}
> event generate .g <<Saved>> -data report.txt
> set data
report.txt

% bind .g <Configure> {set ::size %wx%h}
> .g configure -width 33
> update
> set size
33x20

Errors

% bind
wrong # args: should be "bind window ?pattern? ?command?"
-errorcode TCL WRONGARGS

% bind .f <Foo> x
bad event type or keysym "Foo"
-errorcode NONE

% bind .f <Button-9> x
bad button number "9"
-errorcode NONE

% bind .f <Key-nokey> x
bad event type or keysym "nokey"
-errorcode NONE

% bind .f <Key-a x
missing ">" in binding
-errorcode NONE

% bind .f <<A>><<B>> x
virtual events may not be composed
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also