| 12.1 Focus |
|
|
The focus is defined in The Tk Way of Thinking. 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. There are two important complications:
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. Tk's default mouse mechanism for changing the focus is called
The Tk library currently has no way to change back to the click-to-focus model. 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.
There is a command your scripts can use to assign the focus to a widget. It is used this way. focus WIDGET_NAME
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 Colors below for how to change this indicator.
Tk actually determines the focus in a very complex way. You may wish to skip the rest of this section on first reading. Widgets have a
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. 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 -takefocus. See below in Active/Normal/Disabled. 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. |
Author's Home Page |
|
Order from Amazon. |