Bind#
Binding attaches a callback to an event on a widget. The event pattern names which event fires the callback — its full grammar (types, modifiers, key symbols, and built-in virtual events) is the Events reference. Every widget carries these methods.
The canonical upstream references are the Tk bind and bindtags manual pages (Tcl 8.6).
Binding callbacks#
- bind(sequence=None, func=None, add=None)
Bind
functo the eventsequenceon this widget.funcis called with one event object argument. Return"break"from the callback to stop further handling of that event.- Parameters:
sequence – an event pattern such as
"<Button-1>"or"<<Custom>>".func – the callback; receives one
eventargument.add –
"+"to add to the existing bindings instead of replacing them.
- Returns:
the binding’s function id (a string) — pass it to
unbind— whenfuncis given; otherwise the current binding.
- bind_all(sequence=None, func=None, add=None)
Like
bind, but application-wide — the binding applies to every widget, via theallbind tag.- Returns:
the binding’s function id.
- bind_class(className, sequence=None, func=None, add=None)
Bind at the widget-class level: every widget whose bind tags include
className(e.g."TButton") responds, current and future.- Returns:
the binding’s function id.
- unbind(sequence, funcid=None)
Remove the binding for
sequenceon this widget. Pass thefuncidreturned bybindto remove a single added callback rather than all of them.- Returns:
None.
- unbind_all(sequence)
Remove an application-wide binding made with
bind_all.- Parameters:
sequence (str) – the event sequence to unbind.
- Returns:
None.
- unbind_class(className, sequence)
Remove a class binding made with
bind_class. This affects every widget of that class, including the ones Tk installs by default — removing a built-in class binding takes that behavior away from every widget of the class.
Synthesizing events#
To send an event rather than receive one, use event_generate; custom
<<virtual>> names are managed with event_add / event_delete. These
belong to the event system — their options live in the Events reference.
See also#
Events reference — the pattern grammar, the event object, and the built-in virtual events.
Events & callbacks and the Events feature guide — how to use bindings (scope, stopping propagation, dispatching your own virtual events).