bindtags(n)
π bindtags(n) β the Tk Built-In Commands era π
okay bestie, so you know how bind makes bindings happen? This is the lil command that decides which of those bindings actually fire for a window, and in what order. It's giving backstage-pass energy, ngl. π
β¨ NAME β¨
bindtags β determine which bindings apply to a window, and order of evaluation π¦
πΈ SYNOPSIS πΈ
bindtags window ?tagList?
π DESCRIPTION π
So when you create a binding with the bind command, it gets associated with one of these things: a particular window like .a.b.c, a class name like Button, the keyword all, or literally any other string. All of these forms are called binding tags. Periodt. π
Each window contains a list of binding tags, and that list is what determines how events get processed for that window. When an event occurs in a window, it gets applied to each of the window's tags in order: for each tag, the most specific binding that matches the given tag and event is executed. (Check the bind command for the whole matching-process lore β iykyk.)
By default, each window has four binding tags, consisting of:
- the name of the window
- the window's class name
- the name of the window's nearest toplevel ancestor
all
β¦in that exact order. Toplevel windows only have three tags by default, because the toplevel name is the same as the window's name (no need to say it twice, main character behavior honestly π).
The bindtags command is what lets you read and modify the binding tags for a window.
- If
bindtagsis invoked with only one argument, then the current set of binding tags forwindowis returned as a list. π«Ά - If the
tagListargument is specified tobindtags, then it must be a proper list, and the tags forwindoware changed to the elements of that list.
The elements of tagList may be arbitrary strings β go off, be creative β however, any tag starting with a dot is treated as the name of a window. And if no window by that name exists at the time an event is processed, then that tag is just ignored for that event. No window, no binding, no drama. π
The order of the elements in tagList determines the order in which binding scripts are executed in response to events. Like, it's literally the whole point. For example, this command:
bindtags .b {all . Button .b}
reverses the order in which binding scripts will be evaluated for a button named .b, so that all bindings are invoked first, followed by bindings for .b's toplevel (.) β wait, that's the toplevel literally named . bestie β followed by class bindings, followed by bindings for .b. Iconic little reshuffle. π₯
If tagList is an empty list, then the binding tags for window are returned to the default state described above. A full reset, we love a clean slate. β¨
You can also use bindtags to introduce arbitrary additional binding tags for a window, or to remove standard tags. For example:
bindtags .b {.b TrickyButton . all}
replaces the Button tag for .b with TrickyButton. What does that mean? It means the default widget bindings for buttons β which are associated with the Button tag β will no longer apply to .b, but any bindings associated with TrickyButton (maybe some new button behavior you cooked up π©βπ³) will apply.
π EXAMPLE π
Okay so imagine you have a set of nested frame widgets and you want events sent to a button widget to also be delivered to all the widgets up to the current toplevel β which is in contrast to Tk's default behavior, where events are not delivered to those intermediate windows. Why would you do this? To make it easier to have accelerators that are only active for part of a window. Slay. π
You could use a helper procedure like this one to help set things up:
proc setupBindtagsForTreeDelivery {widget} {
set tags [list $widget [winfo class $widget]]
set w $widget
set t [winfo toplevel $w]
while {$w ne $t} {
set w [winfo parent $w]
lappend tags $w
}
lappend tags all
bindtags $widget $tags
}
π§ SEE ALSO π§
bind(n)β the other half of this whole situation π
β KEYWORDS β
binding, event, tag
fr fr that's the whole page β binding, event, tag, all accounted for, no cap. π¦
this has been a Tk Built-In Commands moment, version 4.0, dated with the bindtags(n) energy β ty for coming to my ted talk, mwah π«Άπ
π¦ where this page comes from
- alpine 3.22: tk-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tk-doc 8.6.17-r0 Β· TCL
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- arch rolling: tk 8.6.16-1 Β· TCL
- centos-stream 10: tk 1:8.6.13-5.el10 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- nixpkgs unstable: tk 9.1.0
- opensuse-leap 16.0: tk 8.6.13-160000.2.2 Β· TCL
- opensuse-leap 16.0: tk 8.6.13-160000.2.2 Β· TCL
- opensuse-leap 16.0: tk 8.6.13-160000.2.2 Β· TCL
- opensuse-leap 16.0: tk 8.6.13-160000.2.2 Β· TCL
- Copyright (c) 1990 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms