Focus#

Keyboard focus decides which widget receives key events. Every widget carries these methods to query and move it. Setting focus is per–toplevel: a widget takes focus within its window, and the window in turn has focus within the display.

The canonical upstream references are the Tk focus and tk_focusNext manual pages (Tcl 8.6).

Setting focus#

focus_set()

Give this widget the keyboard focus the next time its toplevel window has focus. Alias: focus().

Returns:

None.

focus_force()

Take the focus immediately, even if the application’s window does not have focus. Disruptive — prefer focus_set unless you specifically need to steal focus.

Returns:

None.

Querying focus#

focus_get()

Return the widget that currently has focus in this application.

Returns:

the focused widget, or None if no widget in this application has focus.

Return type:

Misc | None

focus_displayof()

Like focus_get, but resolves focus on the display this widget belongs to (relevant only with multiple displays).

Returns:

the focused widget on this display, or None.

Return type:

Misc | None

focus_lastfor()

Return the widget that would receive the focus if this widget’s toplevel regained it — the one that held focus there most recently.

Returns:

the widget that last had focus in this toplevel (the toplevel itself if none did).

Return type:

Misc

Traversal order#

tk_focusNext()

Return the next widget after this one in keyboard-traversal (Tab) order, honoring each widget’s takefocus setting.

Returns:

the next widget in traversal order.

Return type:

Misc

tk_focusPrev()

Return the previous widget before this one in traversal (Shift-Tab) order.

Returns:

the previous widget in traversal order.

Return type:

Misc

tk_focusFollowsMouse()

Switch this application to a focus-follows-mouse model, where focus tracks the pointer instead of requiring a click. There is no call to undo it.

Returns:

None.