bindtags(3tk)
π bindtags(3tk) π
okay so this is a Tk built-in command page, bestie β it's all about binding tags, which is genuinely one of the most underrated little corners of Tk. let's get into it π«Ά
β¨ NAME β¨
bindtags β figure out which bindings apply to a window, and in what order they get evaluated. slay. π
πΈ SYNOPSIS πΈ
bindtags window ?tagList?
π¦ DESCRIPTION π¦
so here's the tea: when you make a binding with the bind command, that binding gets attached to something. and that something is one of these:
- a particular window, like
.a.b.c - a class name, like
Button - the keyword
all - or literally any other string you want
all of these forms are called binding tags. iconic, honestly.
Every window carries around a list of binding tags, and that list is what decides how events get processed for that window. When an event happens in a window, it gets applied to each of the window's tags in order β and for each tag, the most specific binding that matches that tag plus that event is the one that runs. (if you wanna know more about the matching process, go read the bind command, bestie.)
By default, every window has four binding tags, and they are:
- 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 get three tags by default though, because for a toplevel the toplevel name is the window name β no need to say it twice, we're not extra like that.
And that's where bindtags comes in: it lets you read and modify the binding tags for a window. π
If you invoke bindtags with only one argument, you get the current set of binding tags for window back, as a list. cute.
If you pass the tagList argument, it has to be a proper list, and the tags for window get changed to the elements of that list. The elements of tagList can be arbitrary strings! but β heads up β 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 drama, it just gets skipped. π
The order of the elements in tagList determines the order in which binding scripts get executed in response to events. for example:
bindtags .b {all . Button .b}
that reverses the order in which binding scripts get evaluated for a button named .b, so that all bindings are invoked first, followed by bindings for .b's toplevel ("."), followed by class bindings, followed by bindings for .b. literally just flipping the whole lineup. π₯
and if tagList is an empty list? then the binding tags for window get returned to the default state described above. factory reset, but make it fashion. β¨
bindtags can also be used to introduce arbitrary additional binding tags for a window, or to remove standard tags. for example:
bindtags .b {.b TrickyButton . all}
that one replaces the Button tag for .b with TrickyButton. which means the default widget bindings for buttons β the ones 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. it's giving customization. π§
π EXAMPLE π
say you've got 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, note, is in contrast to Tk's default behavior, where events are not delivered to those intermediate windows. the goal here is to make it easier to have accelerators that are only active for part of a window. π―
you could use a helper procedure like this 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
}
no cap, that's the whole trick. π
β SEE ALSO β
π KEYWORDS π
binding, event, tag
and that's the page!! this one comes from Tk, version 4.0 β go forth and reorder your binding tags like the main character you are π πΈβ¨π¦
π¦ where this page comes from
- debian bookworm: tk8.6-doc 8.6.13-2
- debian trixie: tk8.6-doc 8.6.16-1
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu jammy: tk8.6-doc 8.6.12-1build1
- ubuntu noble: tk8.6-doc 8.6.14-1build1
- ubuntu questing: tk8.6-doc 8.6.17-1
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk8.6-doc 8.6.17-1build1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk8.6-doc 8.6.18-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- 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 on : a language model's rewrite of the original, under its terms