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

📄 112.html

📁 Tcl 语言的入门级图书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
or her way.

<A NAME="nmngwdgtbjcts">
<H3>Naming Widget Objects</H3></A>


<P>  You have to choose a name for each widget that you create.  A
<CITE><NAME=#G11.2widgetname>widget name</A></CITE> must be in a form similar to a file's pathname,
for example,

<PRE>
.a
.a.b
.a.b.c
</PRE>

Each new widget name you choose either looks like a period followed by a
string or is formed from an <CITE>existing</CITE> widget's name by appending a period
and a string.  The string may consist of lowercase letters, digits, and the
underscore.  

<P>   The one exception to this form of name is the root window which is itself a
widget named simply "<TT>.</TT>".  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 <TT>.gerbel</TT>, you will give it the widget name <TT>.gerbel</TT>
if it is to be a subwindow of the root window and <TT>.animals.gerbel</TT> if it
is to be a subwindow of a widget whose name is <TT>.animals</TT>.

<P> <STRONG>Remark</STRONG> <DL><DD>

<P>  Widget names can be long.  Choosing names for each component can be a
hassle.  It is possible to generate them automatically.
Of course, this produces widget path names that will be difficult to
remember.

<P>  However, for many widgets there is no need to work with widget commands,
that is, with widget names.  After such widgets are under geometry management, you
can forget them.  Such widget names clearly can be generated automatically.  So
can widget names that will be stored in variables and not used directly.

<P>  When choosing name components by hand, your choices are to choose names the
way you would choose variable names (that is, names that tell something of the
widgets' purpose) or to choose names that reflect the type of widgets that are
involved (for example, button, entry, and so forth).

<P>  When you are storing widget names in variables, the latter choice seems
wiser because you will already have a variable name for the widget.
Otherwise, I do not have a recommendation.
<P> </DL>

<P>  When two widgets have names that differ by one component the way
<TT>.a.b</TT> and <TT>.a.b.c</TT> do, the shorter name identifies the
<CITE><NAME=#G11.2prntwdgt>parent widget</A></CITE> and the longer name identifes the
<CITE><NAME=#G11.2childwidget>child widget</A></CITE>.  When a widget has a name with one
period, for example, <TT>.a</TT>, its parent is the root window.

<P>  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 <CITE><NAME=#G11.2slave>slave</A></CITE> and
the containing window is called the <CITE><NAME=#G11.2master>master</A>.</CITE>  The words "master"
and "slave" indicate which widget has the last word about geometry.

<P> <STRONG>Remark</STRONG> <DL><DD> For another less-common use of the words "master" and
"slave," see above in 
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Executing Scripts from within Scripts</A>.  </DL>

<P>  Thus, "master" and "slave" refer to a relationship on the screen whereas
"parent" and <TT>child</TT> 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
<TT>toplevel</TT> windows which have parents but are not slaves because their
geometry is managed by the window manager rather than Tk.  

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

<A NAME="vrvwfprgrmmngntk">
<H3>Overview of Programming in Tk</H3></A>


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

<P>  You also need to alter the way decisions are made about a widget's
geometry.  You usually do this with the <TT>pack</TT> command.  

<P>   The behavior of some widgets involves executing a script under certain
conditions.  You control what this script is with the <TT>-command</TT> option.
The script will be executed as a result of what the user does with the
keyboard and mouse.  Because of the <TT>-command</TT> option, the Tk programmer
can work at a higher level of abstraction than provided by keystrokes and
mouseclicks.

<P>   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 <TT>bind.</TT>  The <TT>bind</TT>
command relies directly on the user-related events.  The <TT>bind</TT> command
lets you say: "If such and such a user event happens when I am responsible,
then execute this script."

<P>  What is actually going on is that Tk takes user events and uses them to
<CITE><NAME=#G11.2trigger>trigger</A></CITE> appropriate actions.  Some of these actions are built
into Tk.  Others are determined by you with <TT>-command</TT> or <TT>bind</TT>.  None
of these actions is executed immediately.  Instead, the triggering mechanism
places them on a queue, called the <CITE><NAME=#G11.2eventqueue>event queue</A>.</CITE>  Tk's runtime
system executes a continuous process of placing actions on the event queue and
executing the action at the front of the event queue.  This process is
controlled by a loop called the <CITE><NAME=#G11.2eventloop>event loop</A>.</CITE>

<P>  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 HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html"><CITE>A New Command Window</CITE></A>.

<P>  Scripts associated with widgets through the <TT>-command</TT> option and
<TT>bind</TT> command are called <CITE>event handlers</CITE>.  Much, if not
most, of your application's unique behavior comes from its event handlers.

<P>   To recap: programming in Tk means to set up widgets and associated actions.
The associated actions are set up with the <TT>-command</TT> option and the
<TT>bind</TT> command.  The script that sets them up is called the
<CITE>initialization</CITE> 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.

<P>  Two points to keep in mind in designing your Tk scripts:

<UL>

<P> <P><LI> Event handlers do not execute until they have been placed on the event queue.
When they execute, <CITE>event handlers always execute in the top-level
namespace</CITE>. This may be very different than the namespace in which they are
defined.  This difference can be confusing.  For those event handlers which
must use variable substitution in a way that is not very trivial, it is best
to minimize the use of the top-level namespace by putting an event handler's
real action in a procedure.

<P> <P><LI> While an event handler is executing, other events cannot be triggered.
This includes user events and can be very frustrating for a user.  Try to
keep the runtime of your event handlers short.  If you must write an event
handler that will run a long time, provide a visual cue to the user
indicating that some waiting may be necessary.

</UL>

<P> <STRONG>Remark</STRONG> <DL><DD> The event queue and event loop are available in Tcl but
the Tcl event loop does not start executing unless requested and, even when it
does execute, it cannot handle user events.  As an extension of Tcl, Tk adds
the ability to handle these user events and starts with event-driven execution
from the beginning.  You can find more detail about Tcl's basic event loop
above in 
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">The Event Loop</A> and 
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">More about the Event Loop</A> but you do not need that detail to
write most Tk programs.

<P>  In any case, you should know that there are more kinds of events than user
events.  You should know this but you need not think seriously about it at this
time because these other kinds of events are not important in many Tk
applications.  Some of them are are described above with Tcl's event loop.
Others are mentioned briefly with this book's Tk material. These others are
user-related events that tell widgets things like "the focus is being taken
away from you."  </DL>

<!-- 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>Previous</STRONG>
<A HREF="11.1.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.1.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="11.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.3.html">section</A><WBR>
<STRONG>All</STRONG>
<A HREF="11.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.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 + -