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

📄 113.html

📁 Tcl 语言的入门级图书
💻 HTML
字号:
<HTML><TITLE>Overview of Tk: Details of the Caps Example</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Details of the Caps Example</H1></A>


<P>  This section dissects the Caps example, Script S11.1a, to show how
programmers use widgets.

<P>  The script places a text entry area and two buttons in the root window.  The
text entry area is controlled by an entry widget.  Widgets of this type can
take keyboard input from the user and echo it on the screen.  They permit the
user to perform simple editing operations.  These things happen by default.
They can also cause a variable's contents to be kept current with
the text as the user enters it.  For this to happen, the
name of the variable must be declared with a <TT>-textvariable</TT> option.

<P>  Here is the creation of the entry widget as it appears in the Caps
script.

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

The top-level <TT>Entry</TT> variable is not only updated whenever the user edits
the text entry on the screen but the opposite is also true.  Whenever the
<TT>Entry</TT> variable is changed, the screen will reflect the change.

<P>  After it is created, the entry widget is immediately displayed on the screen
with this command:

<PRE>
pack .entry
</PRE>

<P>  Each button has an event handler associated with it that is triggered when
the user clicks on the button.  For the button labelled "Caps," the
event handler is the following script.

<PRE>
set Entry [string toupper $Entry] 
</PRE>

where the variable being processed is the same top-level <TT>Entry</TT> that the
<TT>.entry</TT> widget keeps current.  Binding this event handler to the event of
a mouse click over a button is done with the <TT>-command</TT> option to the
<TT>button</TT> widget maker.  Another option, <TT>-text</TT>, is used to declare the
label.

<P>  Here are the commands that create the <TT>.edit_button</TT> button and map it to
the screen.

<PRE>
button .edit_button -text Caps -command { set Entry [string toupper $Entry] }
pack .edit_button
</PRE>

The effect of clicking this button, of course, is to make all letters in the
<TT>Entry</TT> variable uppercase.   Because of the way this variable is
linked to the <TT>.edit_button</TT> button, the string entered by the user
shows the same alteration.

<P> <P><A NAME="11.3a">
<STRONG>Exercise 11.3a</STRONG> </A><DL><DD>

<P>   How would the functionality of the Caps script be changed if the
<TT>.edit_button</TT> widget were created with

<PRE>
button .edit_button -text Caps -command "set Entry [string toupper $Entry]"
</PRE>

Explain. <P>
<A HREF="11.7.html#Sol11.3a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.7.html#Sol11.3a">Solution</A></DL>


<P> <P><A NAME="11.3b">
<STRONG>Exercise 11.3b</STRONG> </A><DL><DD>
 

<P>   Suppose 

<PRE>
button .edit_button -text Caps -command { set Entry [string toupper $Entry] }
</PRE>

were executed from within a procedure.  Would it be necessary to declare
<TT>Entry</TT> to be global in that procedure?  Why or why not?  <P>
<A HREF="11.7.html#Sol11.3b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.7.html#Sol11.3b">Solution</A></DL>


<P>  The button labelled "Quit" has a very simple event handler associated
with it.  Here are the commands that create this button and map it to the
screen.

<PRE>
button .quit_button -text Quit -command { quit }
pack .quit_button
</PRE>

This event handler merely executes a predefined procedure.  Here is the
implementation of that procedure.

<PRE>
proc quit {} {
   global Entry
   destroy .entry .edit_button .quit_button
   unset Entry
}
</PRE>

This procedure merely gets rid of the three widgets and the top-level
variable created by the initialization script.  It does not exit the
Tk interpreter.


<P> <P><A NAME="11.3c">
<STRONG>Exercise 11.3c</STRONG> </A><DL><DD>

<P>  Alter the Caps script so that instead of removing itself from the <TT>wish??</TT>
interpreter, it simply terminates the interpreter.  Simplify.
<P> <P>
<A HREF="11.7.html#Sol11.3c" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.7.html#Sol11.3c">Solution</A></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.2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.2.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="11.4.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.4.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 + -