| 11.2 The Tk Way of Thinking |
|
|
The wish?? program itself is an example of what a GUI application can be. When the Caps script of the previous section is running, you can
If these programmers had been working in the traditional way, they would have a horrible tangle of input possibilities to take care of. But GUI applications are not written in the traditional "top down," "one step at a time" manner. Instead, the GUI runtime system generates events and the programmer simply associates actions with those events that are of concern. The associations work something like the following.
Window ManagersFor the Tk programmer, the GUI runtime system consists of two pieces:
The work of going from a key press, key release, mouse movement, or mouse click event to the application's action for that event is largely done by these two runtime systems. The Tk programmer works at a much higher level of abstraction. As the Caps example shows, you can write entire applications without needing to deal directly with keystrokes or mouse clicks. A function of the window manager is to send messages to Tk about key presses, key releases, mouse movements, or mouse clicks. I call these messages user events to distinguish them from I/O events. You have probably noticed that you cannot enter text for the Caps example
unless you first click on the text entry subwindow. The concept that is
important here is that of Tk's default way of handling the focus is to leave the focus unchanged as the mouse moves it takes a mouse click before Tk will change the focus. Your window manager, however, may handle the focus differently. Thus you could see the focus switching between applications as you move the mouse, but not switching within a Tk application unless you click the mouse. A second function of the window manager is to control the position and size
of all windows. Tk borrows the term A third function of the window manager is to put windows into a kind of picture frame that includes a title. You can have some effect on the way the window manager does its job by
sending requests to it through the appropriate Tk commands. Remember though,
that the window manager is not Tk and it varies from platform to platform or,
even, from X Windows user to X Windows user. Programming in a way that counts
on window manager behavior can be a mistake.
Tk's basic abstraction is the
Tk gives you four ways of dealing with individual widgets:
A widget's appearance and behavior are determined by the values of its options. The default values are altered with the widget making command or with a configure widget command. Most defaults do not need to be altered to get some kind of working application running. A few do. The default action associated with clicking a button, for example, is to do nothing. You will hardly get a working application with that behavior. In general, geometry is something imposed on a window or subwindow from
above. However, the entity doing the imposing can be made aware of the window
or subwindow's preferences. For example, if you use the -width option to
declare the number of characters an entry widget should display, this knowledge
percolates up to a size request that is sent to the window manager. The
window manager knows what geometry is wanted, but, if the user requests
something else, the window manager may very well let the user have his
or her way.
You have to choose a name for each widget that you create. A
.a .a.b .a.b.cEach new widget name you choose either looks like a period followed by a string or is formed from an existing widget's name by appending a period and a string. The string may consist of lowercase letters, digits, and the underscore. The one exception to this form of name is the root window which is itself a widget named simply ".". In fact, the period at the beginning of a widget's name represents the root window. So if you want a normal widget name to end with .gerbel, you will give it the widget name .gerbel if it is to be a subwindow of the root window and .animals.gerbel if it is to be a subwindow of a widget whose name is .animals. Remark
When two widgets have names that differ by one component the way
.a.b and .a.b.c do, the shorter name identifies the
When two widgets are mapped to the screen in such a way that one is a direct
subwindow of the other, the subwindow is called the Remark
Thus, "master" and "slave" refer to a relationship on the screen whereas "parent" and child refer to a relationship built into the names. For widgets that, in fact, are represented on the screen, "parent" and "master" are almost always equivalent, as are "child" and "slave." There are only two ways in which this equivalence can be violated. The first involves toplevel windows which have parents but are not slaves because their geometry is managed by the window manager rather than Tk. The second violation of this equivalence involves an option for using Tk's
window managers that is not described in this book. The main purpose of this
option seems to be to enable widget names to be shorter. I do not think
that is necessary. If a widget name needs to be referenced a lot, then put it
in a variable with a short name. That will keep things simple.
You program in Tk mainly by creating widgets and altering their default appearance and behavior. You do this mainly by passing options to the first two kinds of widget processing commands. You also need to alter the way decisions are made about a widget's geometry. You usually do this with the pack command. The behavior of some widgets involves executing a script under certain conditions. You control what this script is with the -command option. The script will be executed as a result of what the user does with the keyboard and mouse. Because of the -command option, the Tk programmer can work at a higher level of abstraction than provided by keystrokes and mouseclicks. Sometimes, however, you want to work at a lower level of abstraction, that is, you want more direct control over what happens when the user does things. You get this control with a nonwidget command called bind. The bind command relies directly on the user-related events. The bind command lets you say: "If such and such a user event happens when I am responsible, then execute this script." What is actually going on is that Tk takes user events and uses them to
With Tk, everything that happens is controlled by this event loop. Even processing of commands you enter into the command window is controlled by the event loop. This will seem strange at first but you will find an explanation of how such things can happen below in A New Command Window. Scripts associated with widgets through the -command option and bind command are called event handlers. Much, if not most, of your application's unique behavior comes from its event handlers. To recap: programming in Tk means to set up widgets and associated actions. The associated actions are set up with the -command option and the bind command. The script that sets them up is called the initialization script. The initialization script can also do Tcl things, such as load data. When you run Tk interactively, what you are typing into the command window is the initialization script. Two points to keep in mind in designing your Tk scripts:
Remark
|
Author's Home Page |
|
Order from Amazon. |