⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 112.html

📁 Tcl 语言的入门级图书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><TITLE>Overview of Tk: The Tk Way of Thinking</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>The Tk Way of Thinking</H1></A>


<P>  The <TT>wish??</TT> program itself is an example of what a GUI application can be.
When the Caps script of the previous section is running, you can

<UL>
<P><LI> enter another command in the command window
<P><LI> enter text in the root window
<P><LI>  click on either button in the root window
</UL>

The choice of what to do belongs to you not the people who wrote the <TT>wish??</TT>
interpreter and the Caps script.

<P>  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 <CITE>events</CITE> and
the programmer simply associates actions with those events that are of
concern.  The associations work something like the following.

<DL><DD>
<P>    For a keystroke belonging to the command window, do ...

<P>    For a keystroke belonging to the root window's text entry area, do ...

<P>    For a mouse click over the "Caps" button, do ...

<P>    For a mouse click over the "Quit" button, do ...
</DL>

<A NAME="wndwmngrs">
<H3>Window Managers</H3></A>


<P>  For the Tk programmer, the GUI runtime system consists of two pieces:

<OL>

<P><LI> The <CITE>window manager</CITE> provided by the operating system or a GUI 
overlay to the operating system. (My use of the term is more inclusive
than users of X Windows are used to.  Here "window manager" includes
both X Windows and any X Windows window manager that may be running
under X Windows.)

<P><LI>  The Tk runtime system.

</OL>

<P>   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.

<P>   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 <CITE>user events</CITE> to distinguish them from I/O events.

<P>   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 <CITE><NAME=#G11.2focus>focus</A>.</CITE> One window at a time is said
to have the focus.  This means that keystrokes are directed toward that
window.  Focus is determined at two levels. The window manager determines
which window should have the focus.  It gives the focus to the
program running that window.  This program in the case of the Caps example is
<TT>wish.</TT>  The <TT>wish??</TT> program then determines which of its windows or
subwindows should have the focus.

<P>  Tk's default way of handling the focus is to leave the focus unchanged as
the mouse moves &#150; 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.

<P>  A second function of the window manager is to control the position and size
of all windows.  Tk borrows the term <CITE><NAME=#G11.2geometry>geometry</A></CITE> from X Windows to
refer to a window's position and size.  When you use your mouse to move a
window around or resize it, the mouse is working with the window
manager.  The window manager then tells Tk what has been decided and Tk does
its best with that decision.

<P>  A third function of the window manager is to put windows into a kind of
picture frame that includes a title.

<P>  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.

<A NAME="widgets">
<H3>Widgets</H3></A>


<P>   Tk's basic abstraction is the <CITE><NAME=#G11.2widget>widget</A>. </CITE>  A widget is a
potential subwindow (or occasionally a window) that serves some purpose.  To
serve this purpose a widget has default ways of:

<UL>

<P><LI>  displaying itself on the screen 
<P><LI>  dealing with mouse and keyboard input that are directed to it

</UL>

In the Caps example, there are three widgets, one for each of the two buttons
and one for the entry area.  The button widgets have each been assigned an
action to execute when they are clicked.  No actions have been assigned
to the entry widget, its default behavior is enough for this example.


<P>  Tk gives you four ways of dealing with individual widgets:

<OL>

<P><LI> You can create a widget of a given widget type.  This is done with a
widget making command that is named after the widget type, for example, entry, button,
or label.  As you use this command you must name the new widget.

<P>  For example, here is a command line to create an entry widget.

<PRE>
entry .entry -textvariable Entry
</PRE>

The widget's name is <TT>.entry</TT> and the <TT>-textvariable</TT> option is given a
nondefault value of "Entry" which is the name of a  variable in the
top-level namespace.

<P><LI> You can alter characteristics of a widget that has already been created.
This is possible through the actions of an object action family that is
created with the widget.  This action family is implemented with a procedure
whose name is the same as the name of the widget.  This procedure is called a
<CITE><NAME=#G11.2wdgtcmmnd>widget command</A></CITE> but the term also seems to apply to particular
actions within the object action family.

<P> This book distinguishes between three kinds of Tk commands:
<NAME=#G11.2widgetmaker>widget maker</A>s, widget commands, and <CITE><NAME=#G11.2nnwdgtcmmnds>nonwidget commands</A></CITE>.  The latter is a catchall category for Tk commands that do not fit
into either of the first two classifications.

<P> For example, here is a <TT>configure</TT> action that will change the value of
the <TT>-textvariable</TT> option of the widget named <TT>.entry</TT>.

<PRE>
.entry configure -textvariable SecondEntry
</PRE>

<P><LI> You can lay down guidelines concerning the size and position of a widget
within another subwindow or  window.  You will almost always do this
with the <TT>pack</TT> command.  

<P>  Any procedure such as <TT>pack</TT> that helps you fit a widget into another is
called a <CITE><NAME=#G11.2gmtrymngr>geometry manager</A>.</CITE>  Without a geometry manager, your
widgets cannot be seen on the screen.

<P>  Here is the simplest way to put the <TT>.entry</TT> widget under geometry
management:

<PRE>
pack .entry
</PRE>

<P><LI> You can destroy a widget. This is done with the <TT>destroy</TT> command.

<P>  For example,

<PRE>
destroy .entry
</PRE>

</OL>

<P>  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 <TT>configure</TT> 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.

<P>  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 <TT>-width</TT> 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -