A New Command for Binding to Tk EventsJeff Rogers$Revision: 1.2 $
This TIP proposes a more modern mechanism for binding callbacks to Tk's events.
The Tk bind' command passes details about an event to a callback script by doing a textual substitution of percent markers. This has worked well for years, however most recent code prefers to use command prefixes to which set arguments are appended rather than scripts which are evaluated. This TIP proposes such an approach for tk event binding.
A new command, "tkevent" is introduced with the following syntax:
tkevent tag sequence cmd
The tag and sequence arguments are the same as used in the bind command. The cmd is evaluated by appending a single argument which is a dictionary containing the event details. The implementation of cmd can retrieve details of the event using that dictionary.
Bindings created by tkevent are compatible with those created by bind. When a sequence is bound to a tag using tkevent, it replaces any previous binding, and vice versa. Appending to a binding with "bind tag sequence +script" may not work as expected.
The possible keys in the dict passed to the handler are:
ICAgICAgICBzZXJpYWwgICAgICAgIGFib3ZlICAgICAgICAgYnV0dG9uICAgICAgICBjb3VudCAgICAgICAgIGRldGFpbCAgICAgICAgZm9jdXM=ICAgICAgICBoZWlnaHQgICAgICAgIHdpbmRvdyAgICAgICAga2V5Y29kZQ==ICAgICAgICBtb2RlICAgICAgICAgIG92ZXJyaWRlX3JlZGlyZWN0ICAgICAgICBwbGFjZSAgICAgICAgIHN0YXRlICAgICAgICAgdGltZQ==ICAgICAgICB3aWR0aCAgICAgICAgIHggICAgICAgICAgICAgeQ==ICAgICAgICBjaGFyYWN0ZXIgICAgIGJvcmRlcl93aWR0aCAgZGVsdGE=ICAgICAgICBzZW5kX2V2ZW50ICAgIGtleXN5bSAgICAgICAga2V5c3ltX251bQ==ICAgICAgICBwcm9wZXJ0eSAgICAgIHJvb3QgICAgICAgICAgc3Vid2luZG93ICAgICAgICB0eXBlICAgICAgICAgIHdpbmRvdyAgICAgICAgeHJvb3Q=ICAgICAgICB5cm9vdA==
These keys are intended to be be the same as the options to event generate where applicable.
Not all values are legal for all event types; where a key is not legal for an event type, it will not be present in the dictionary when the cmd bound to that event is evaluated.
A sample implementation of these commands in pure tcl is available at
Except for the "+script" feature, bind and tkevent support identical functionality, and either could be implemented in terms of the other. If bind as a core command was dropped in favor of tkevent, it could be provided as a library implementation.
This document has been placed in the public domain.