1focus(n) Tk Built-In Commands focus(n)
2
3
4
5______________________________________________________________________________
6
8 focus - Manage the input focus
9
11 focus
12 focus window
13 focus option ?arg arg ...?
14______________________________________________________________________________
15
17 The focus command is used to manage the Tk input focus. At any given
18 time, one window on each display is designated as the focus window;
19 any key press or key release events for the display are sent to that
20 window. It is normally up to the window manager to redirect the focus
21 among the top-level windows of a display. For example, some window
22 managers automatically set the input focus to a top-level window when‐
23 ever the mouse enters it; others redirect the input focus only when
24 the user clicks on a window. Usually the window manager will set the
25 focus only to top-level windows, leaving it up to the application to
26 redirect the focus among the children of the top-level.
27
28 Tk remembers one focus window for each top-level (the most recent
29 descendant of that top-level to receive the focus); when the window
30 manager gives the focus to a top-level, Tk automatically redirects it
31 to the remembered window. Within a top-level Tk uses an explicit focus
32 model by default. Moving the mouse within a top-level does not nor‐
33 mally change the focus; the focus changes only when a widget decides
34 explicitly to claim the focus (e.g., because of a button click), or
35 when the user types a key such as Tab that moves the focus.
36
37 The Tcl procedure tk_focusFollowsMouse may be invoked to create an
38 implicit focus model: it reconfigures Tk so that the focus is set to a
39 window whenever the mouse enters it. The Tcl procedures tk_focusNext
40 and tk_focusPrev implement a focus order among the windows of a top-
41 level; they are used in the default bindings for Tab and Shift-Tab,
42 among other things.
43
44 The focus command can take any of the following forms:
45
46 focus Returns the path name of the focus window on the display con‐
47 taining the application's main window, or an empty string if no
48 window in this application has the focus on that display.
49 Note: it is better to specify the display explicitly using
50 -displayof (see below) so that the code will work in applica‐
51 tions using multiple displays.
52
53 focus window
54 If the application currently has the input focus on window's
55 display, this command resets the input focus for window's dis‐
56 play to window and returns an empty string. If the application
57 does not currently have the input focus on window's display,
58 window will be remembered as the focus for its top-level; the
59 next time the focus arrives at the top-level, Tk will redirect
60 it to window. If window is an empty string then the command
61 does nothing.
62
63 focus -displayof window
64 Returns the name of the focus window on the display containing
65 window. If the focus window for window's display is not in this
66 application, the return value is an empty string.
67
68 focus -force window
69 Sets the focus of window's display to window, even if the appli‐
70 cation does not currently have the input focus for the display.
71 This command should be used sparingly, if at all. In normal
72 usage, an application should not claim the focus for itself;
73 instead, it should wait for the window manager to give it the
74 focus. If window is an empty string then the command does noth‐
75 ing.
76
77 focus -lastfor window
78 Returns the name of the most recent window to have the input
79 focus among all the windows in the same top-level as window. If
80 no window in that top-level has ever had the input focus, or if
81 the most recent focus window has been deleted, then the name of
82 the top-level is returned. The return value is the window that
83 will receive the input focus the next time the window manager
84 gives the focus to the top-level.
85
87 When an internal window receives the input focus, Tk does not actually
88 set the X focus to that window; as far as X is concerned, the focus
89 will stay on the top-level window containing the window with the focus.
90 However, Tk generates FocusIn and FocusOut events just as if the X
91 focus were on the internal window. This approach gets around a number
92 of problems that would occur if the X focus were actually moved; the
93 fact that the X focus is on the top-level is invisible unless you use C
94 code to query the X server directly.
95
97 To make a window that only participates in the focus traversal ring
98 when a variable is set, add the following bindings to the widgets
99 before and after it in that focus ring:
100 button .before -text "Before"
101 button .middle -text "Middle"
102 button .after -text "After"
103 checkbutton .flag -variable traverseToMiddle -takefocus 0
104 pack .flag -side left
105 pack .before .middle .after
106 bind .before <Tab> {
107 if {!$traverseToMiddle} {
108 focus .after
109 break
110 }
111 }
112 bind .after <Shift-Tab> {
113 if {!$traverseToMiddle} {
114 focus .before
115 break
116 }
117 }
118 focus .before
119
121 events, focus, keyboard, top-level, window manager
122
123
124
125Tk 4.0 focus(n)