📄 121.html
字号:
<HTML><TITLE>Widget Characteristics: Focus</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Focus</H1></A>
<P> The focus is defined in
<A HREF="11.2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.2.html">The Tk Way of Thinking</A>. The purpose of the concept is to
determine which window gets keystroke events. At one level, the concept is
simple, the unique widget with the focus gets the keystrokes. Widgets get the
focus when you click the mouse on them or when you use the tab key to cycle
the focus to them.
<P> There are two important complications:
<OL>
<P> <P><LI> Tk's focus mechanism is different than the window manager's focus
mechanism. The window manager gives the focus to one of the windows it is
managing in its own way. Tk gives the focus to at most one widget in any of
its windows to which the window manager has given the focus. This
section discusses only Tk's part of the focus mechanism.
<P> <P><LI> When a given widget has the focus, some keystroke events are not sent
to it. This is because some bindings to top-level windows are effective when
any part of the window has the focus and because some bindings are effective
for all keystrokes.
<P> This was illustrated with the enhanced Caps example of Figure 11.4a.
When the entry widget in that example had the focus, printable characters went
to the entry widget. Tab keyboard events, however, did not go to the entry
widgets. Another binding took effect.
<P> This feature is a part of a mechanism described below in
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Bindings</A>.
</OL>
<P>
<P> There are other complications but they aren't important unless you want
to change the way in which the focus is assigned to a widget.
<P> Tk's default mouse mechanism for changing the focus is called
<CITE><NAME=#G12.1clcktfcs>click to focus</A></CITE>. There is another common way that GUI systems
change the focus, it is called
<CITE><NAME=#G12.1fcsfllwsms>focus follows mouse</A></CITE>. The names say it all. To change Tk so
that it will use the focus-follows-mouse model, execute
<PRE>
<NAME=#Ctkfcsfllwsms>tk_focusFollowsMouse</A>
</PRE>
<P> The Tk library currently has no way to change back to the click-to-focus
model.
<P> Alongside one of these mouse mechanisms for changing the focus is the
keyboard mechanism that uses the Tab key to cycle through a set of widgets that
may accept the focus. Holding the shift key down when using the Tab key
reverses the cycling direction. Of course, for this mechanism to work, the
binding to Tab key events cannot be altered.
<P>
<P> There is a command your scripts can use to assign the focus to a widget. It
is used this way.
<PRE>
focus <CITE>WIDGET_NAME</CITE>
</PRE>
<P> <P><A NAME="12.1a">
<STRONG>Exercise 12.1a</STRONG> </A><DL><DD>
<P> Revise the script in Figure 11.4a so that Tk will always pass the focus
along to the entry widget.
<P> <P>
<A HREF="12.8.html#Sol12.1a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/12.8.html#Sol12.1a">Solution</A></DL>
<P> As you have seen by now, Tk provides a visual indication of which widget has
the focus. This indicator is set up by default for those widgets that get
the focus by default and not for those that do not. Look in
<A HREF="12.4.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/12.4.html">Colors</A>
below for how to change this indicator.
<P>
<P> Tk actually determines the focus in a very complex way. You may wish
to skip the rest of this section on first reading.
<P> Widgets have a <TT><NAME=#S12.1takefocus>-takefocus</A></TT> option to determine whether they will
accept the focus. When they are visible and this option is true, they can
accept the focus. The <TT>-takefocus</TT> option has three possible
values: true, false, and the empty string. The default tends to be the empty
string. The value of this option is used inconsistently with different
kinds of widgets and the three ways of letting the user pick the focus:
the Tab key, focus follows mouse, and click to focus. The next
exercise exhibits some of the inconsistencies.
<P> <P><A NAME="12.1b">
<STRONG>Exercise 12.1b</STRONG> </A><DL><DD>
Create three button widgets labelled "Default,"
"Takes Focus," and "Doesn't Take Focus" and make sure the value of their
<TT>-takefocus</TT> option matches these labels. Also create three entry widgets
whose <TT>-takefocus</TT> option likewise takes on the three possible values. Pack
these widgets so that the two default <TT>-takefocus</TT> values come first, then
the two true values, then the two false values.
<P> See how the focus changes when you use the Tab key.
<P> See how the focus changes when you click the left mouse button.
<P> Execute <TT>tk_focusFollowsMouse</TT> and see how the focus changes
as you move the mouse cursor. Remember that once you do this you
cannot return to the click-to-focus method.
<P>
<A HREF="12.8.html#Sol12.1b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/12.8.html#Sol12.1b">Solution</A></DL>
<P> Some of the inconsistency you see in doing this experiment can be explained
by saying that if you want to create an application that permits buttons to be
pushed without using the mouse, the Tab mechanism must be able to pass the
focus to buttons. On the other hand, if the user is pushing buttons with the
mouse, it is probably distracting to see the focus switch when the button is
clicked – unless the focus is following the mouse anyway.
<P> Another part of the inconsistency can be fixed by noting that widgets
that permit keyboard entry of data have another option that acts a lot
like <TT>-takefocus</TT>. See below in
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Active/Normal/Disabled</A>.
<P> I do not feel that either of these considerations justifies the complex
inconsistencies that surround Tk's focus mechanism. Luckily, the issue
does not seem important for most applications.
<!-- Linkbar -->
<P><CENTER><FONT SIZE=2><NOBR>
<STRONG>From</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/sbf/tcl/book/home.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/sbf/tcl/book/home.html'" tppabs="http://www.mapfree.com/sbf/tcl/book/home.html">Tcl/Tk For Programmers</A><WBR>
<STRONG>Next</STRONG>
<A HREF="12.2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/12.2.html">section</A><WBR>
<STRONG>All</STRONG>
<A HREF="12.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/12.html">sections</A><WBR>
<STRONG>Author</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/mp/jaz/home.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/mp/jaz/home.html'" tppabs="http://www.mapfree.com/mp/jaz/home.html">J. A. Zimmer</A><WBR>
<STRONG>Copyright</STRONG>
<A HREF="copyright.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/copyright.html">Notice</A><WBR>
<P>
<I>Jun 17, 1998</I>
</NOBR></FONT></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -