focus(3tk)
๐ NAME ๐
ok so bestie, this is focus from Tk's built-in commands โ it's the one that manages the input focus. that's the whole vibe. ๐ฏ
โจ SYNOPSIS โจ
focus
focus window
focus option ?arg ...?
๐ DESCRIPTION ๐
The focus command is the one you call when you need to wrangle the Tk input focus. fr fr, at any given moment, exactly one window on each display is the designated focus window โ and every key press or key release event for that display gets yeeted straight to it. ๐
Now, normally it's the window manager's job to redirect the focus around the top-level windows of a display. and like, every window manager has its own personality here: some of them will auto-set the input focus to a top-level window the second your mouse wanders into it, while others are lowkey stubborn and only redirect the focus when you actually click on a window. usually the window manager only bothers setting the focus to top-level windows, and then it's on the application to redirect the focus among the children of that top-level. ๐ช
Here's the part that's giving main character energy: Tk remembers one focus window for each top-level โ specifically, the most recent descendant of that top-level to receive the focus. so when the window manager hands the focus to a top-level, Tk automatically redirects it to that remembered window. ๐ซ within a top-level, Tk uses an explicit focus model by default. meaning: moving the mouse around inside a top-level does not normally change the focus. nope. the focus changes only when a widget explicitly decides to claim it (like, say, because of a button click), or when the user types a key such as Tab that moves the focus. ๐ฆ
The Tcl procedure tk_focusFollowsMouse can be invoked if you want an implicit focus model instead: it reconfigures Tk so that the focus is set to a window whenever the mouse enters it. ๐ญโจ and the Tcl procedures tk_focusNext and tk_focusPrev implement a focus order among the windows of a top-level; they're what's used in the default bindings for Tab and Shift-Tab, among other things. iykyk.
The focus command can take any of the following forms:
focusโ Returns the path name of the focus window on the display containing the application's main window, or an empty string if no window in this application has the focus on that display.
๐ note: it is better to specify the display explicitly using -displayof (see below) so that the code will work in applications using multiple displays. no cap, do the explicit thing.
focus windowโ If the application currently has the input focus on window's display, this command resets the input focus for window's display to window and returns an empty string. If the application does not currently have the input focus on window's display, window will be remembered as the focus for its top-level; the next time the focus arrives at the top-level, Tk will redirect it to window. If window is an empty string then the command does nothing. (nothing. zero. nada. ๐)focus -displayof windowโ Returns the name of the focus window on the display containing window. If the focus window for window's display is not in this application, the return value is an empty string.focus -force windowโ Sets the focus of window's display to window, even if the application does not currently have the input focus for the display.
โ ๏ธ bestie, this command should be used sparingly, if at all. in normal usage, an application should not claim the focus for itself; instead it should wait for the window manager to give it the focus. like, be patient, the wm will get there. ๐ If window is an empty string then the command does nothing.
focus -lastfor windowโ Returns the name of the most recent window to have the input focus among all the windows in the same top-level as window. If no window in that top-level has ever had the input focus, or if the most recent focus window has been deleted, then the name of the top-level is returned. this return value is the window that will receive the input focus the next time the window manager gives the focus to the top-level. ๐ฎ
๐ซถ QUIRKS ๐ซถ
ok so this is the funky part. When an internal window receives the input focus, Tk does not actually set the X focus to that window. as far as X is concerned? the focus stays sitting on the top-level window that contains the window with the focus. ๐
BUT โ and this is the slay โ Tk still generates FocusIn and FocusOut events just as if the X focus were on the internal window. โจ this approach gets around a whole pile of problems that would happen if the X focus were actually moved. the fact that the X focus is on the top-level is invisible unless you go use C code to query the X server directly. sneaky. love that for us. ๐
๐ EXAMPLE ๐
say you want a window that only participates in the focus traversal ring when a variable is set โ add the following bindings to the widgets before and after it in that focus ring:
button .before -text "Before"
button .middle -text "Middle"
button .after -text "After"
checkbutton .flag -variable traverseToMiddle -takefocus 0
pack .flag -side left
pack .before .middle .after
bind .before <Tab> {
if {!$traverseToMiddle} {
focus .after
break
}
}
bind .after <Shift-Tab> {
if {!$traverseToMiddle} {
focus .before
break
}
}
focus .before
๐ง KEYWORDS ๐ง
events, focus, keyboard, top-level, window manager
and that's the whole page, bestie ๐ this was Tk 4.0, focus(3tk) โ written from the Tk Built-In Commands corner of the manual. go forth and focus responsibly. ๐ธ๐โจ
๐ฆ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1990-1994 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