📄 111.html
字号:
<HTML><TITLE>Overview of Tk: Starting with Tk</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Starting with Tk</H1></A>
<P> Tk's interpreter is called <TT>wish??</TT>. The letters stand for
"windowing shell" the question marks represent the version number
which may not be present on your system. Figure 11.1a shows a simple
example that also appears above in
<A HREF="1.2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/1.2.html">Interactive Execution</A>.
<P> When run interactively, the <TT>wish??</TT> interpreter begins with two windows. One
of these is the command window you are used to from working with Tcl. The
other is the root window. The command window will show Tcl's usual command
prompt. The root window will be empty. Figure 11.1a shows what the root
window will look like if the script shown there is entered into the command
window or executed directly.
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 11.1a: Putting a label in the root window.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F11x1a.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F11x1a.JPG">
</TD></TR>
<TR><TD><PRE>label .example -text "Hellow Orld"
pack .example </PRE></TD></TR>
</TABLE></CENTER><P>
<P> The <TT>label</TT> command in this script creates a <CITE>widget,</CITE> a potential
subwindow, whose purpose is to display a label. The last two arguments in
this command line declare an option that provides text for this label.
The string <TT>.example</TT> names the widget. This name is returned by
the <TT>label</TT> command.
<P> The <TT>pack</TT> command arranges for the newly created label widget to be
mapped to the screen as a part of the root window.
<P> The two commands of this script could have been written this way:
<PRE>
pack [label .example -text {Hellow Orld}]
</PRE>
<P> As you learn Tk you will probably run interactively and experiment from the
command window. Later you will create scripts to execute directly so users will
not need to work with a Tcl command window. Running interactively, you will
want a way to trash your subwindows that does not shut down the <TT>wish??</TT>
interpreter. Simply type
<PRE>
<NAME=#Cdestroy>destroy</A> <CITE>WIDGET_NAME</CITE>
</PRE>
For the "Hello World" application, you would type
<PRE>
destroy .example
</PRE>
<P> You can enter any Tcl command into the command window because Tk is
essentially Tcl with some commands added to support a GUI environment. These
extra commands let you set up the Tk widgets and map them to the screen.
<P> Much of a Tk script executes because of things a user does with the widgets
in the root window. The commands that you enter into the command window or
execute directly merely set things up so the proper responses will be made to
the user's manipulation of the widgets with keyboard and mouse.
<P> The root window and command window are examples of <CITE><NAME=#G11.1window>window</A>s</CITE>.
These are coherent rectangles on your screen that you can resize and minimize
or iconize. Besides the root and command window, the other Tk windows you
will deal with are menus, as described below in
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Menus and Menubuttons</A>, or
<TT>toplevel</TT> windows, as described below in
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Toplevel</A>. Much of Tk
programming involves populating windows with subwindows.
<P> Figure 11.1b shows another initialized root window. At the top is an area
where you could enter some keyboard characters. When you click the button in
the middle, any letters you have typed will be converted to uppercase. Click
the button at the bottom and the initialization is undone, leaving you
with a raw root window.
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 11.1b: The caps example.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F11x1b.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F11x1b.JPG">
</TD></TR>
</TABLE></CENTER><P>
<P> The script that creates the Caps example appears separately as Script
S11.1a. Placing it in a separate script box rather than with a figure
makes it available from the book's
<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">home page.</A> Also,
I prefer to attach scripts to figures when I am ready to explain them.
I am not yet ready to explain S11.1a. However, to get used to Tk and acquire the right
mindset for reading the next section, it would be a good idea for you
to execute this
script both interactively and directly.
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR> Script S11.1a: The initialization script for the Caps example. </ADDR></CAPTION
<TR><TD><PRE>## Script S11.1a from "Tcl/Tk For Programmers"
proc quit {} {
<P> global Entry
<P> destroy .entry .edit_button .quit_button
<P> unset Entry
}
entry .entry -textvariable Entry
pack .entry
button .edit_button -text Caps -command { set Entry [string toupper $Entry] }
pack .edit_button
button .quit_button -text Quit -command { quit }
pack .quit_button
</PRE></TD></TR>
</TABLE></CENTER></P>
<P> <STRONG>Remark</STRONG> <DL><DD>
<P> The batch-file method of executing <TT>tclsh??</TT> that is shown above
in
<A HREF="1.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/1.3.html">Direct Execution</A> is not necessary for <TT>wish??</TT> – you can use the simpler
method shown there.
</DL>
<P> <P><A NAME="11.1a">
<STRONG>Exercise 11.1a</STRONG> </A><DL><DD>
Execute Script S11.1a both interactively and directly. At
this point, do not worry about what is in the script – that is explained below
in
<A HREF="11.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.3.html">Details of the Caps Example</A>.
<P> As you experiment with the way the script runs, notice what the Tab key
does. The function of the Tab key will be changed in the
<A HREF="11.4.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.4.html">Enhancing Caps with Tab</A> section.
<P>
<A HREF="11.7.html#Sol11.1a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.7.html#Sol11.1a">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>Next</STRONG>
<A HREF="11.2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/11.2.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 + -