📄 ch30.htm
字号:
puts [expr 2 * $i]
set i [expr $i + 1]
}
</FONT></PRE>
<P>This <TT>while</TT> loop performs the same function as the example that was presented
in the section describing the <TT>for</TT> loop. It calculates 2 x <TT>i</TT> each
time through the loop and prints the result to the screen. Notice that in this example
you have to handle incrementing the counter yourself. With the <TT>for</TT> loop,
the counter incrementing was taken care of by the <TT>for</TT> command.
<H4 ALIGN="CENTER"><A NAME="Heading16<FONT COLOR="#000077">The switch Command</FONT></H4>
<P>The <TT>switch</TT> command provides the same function as an <TT>if</TT> statement
that has multiple <TT>elseif</TT> clauses associated with it. The <TT>switch</TT>
command compares a value (this value is usually stored in a variable) with any number
of patterns, and if it finds a match it executes the Tcl code associated with the
matching pattern.<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">switch $thing {
car {puts "thing is a car"}
truck {puts "thing is a truck"}
default {puts "I don't know what this thing is"}
}
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading17<FONT COLOR="#000077"><B>NOTE:</B> </FONT>The Tcl <TT>switch</TT>
command is equivalent to the <TT>case</TT> statement found in Pascal and some other
languages.
<HR>
</DL>
<P>This <TT>switch</TT> command compares the value that is stored in the <TT>thing</TT>
variable (which must be set prior to these statements, of course) with the string
<TT>car</TT> and the string <TT>truck</TT> to see if it matches either of them. If
the value of the <TT>thing</TT> variable is equal to <TT>car</TT>, then <TT>thing
is a car</TT> is displayed on the screen. If the value of the <TT>thing</TT> variable
is equal to <TT>truck</TT>, then <TT>thing is a truck</TT> is displayed on the screen.
If neither of these cases are true, the default clause is executed and <TT>I don't
know what this thing is</TT> displays on the screen.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading18<FONT COLOR="#000077"><B>TIP:</B> </FONT>Whenever you need
to check to see if a variable is equal to one of a number of values, you should use
a <TT>switch</TT> command instead of an <TT>if</TT> command with multiple <TT>elseif</TT>
clauses. This makes your code much easier to read and understand.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading19<FONT COLOR="#000077">Comments</FONT></H4>
<P>It is always a good idea to include comments in any Tcl code you write--or code
you write in any other language, for that matter. This becomes especially important
if any of the following situations is possible:
<UL>
<LI>Someone else needs to look at or maintain your code.
<P>
<LI>Your programs get large.
<P>
<LI>You won't be looking at code that you have written for long periods of time after
you write it.
</UL>
<P>Chances are that at least one of these situations will come up with Tcl code you
have written.</P>
<P>Comments cannot be placed in the middle of a command. They must occur between
commands. The pound sign (#) is used to inform Tcl to expect a comment.<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF"># This is a valid comment
set a 1 ; # This is a valid comment
set a 1 # This is an invalid comment
</FONT></PRE>
<P>The third comment shown here is invalid because it occurs in the middle of a command.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading20<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Recall that Tcl
interprets everything up to a newline character or a semicolon to be part of the
command.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading21<FONT COLOR="#000077">The Tk Language
Extensions</FONT></H3>
<P>Earlier in this chapter, a simple example of Tk displayed <TT>Hello there</TT>
in a button in the <TT>wish</TT> window. Tk is much more powerful than that example
showed. Along with the button widget are many other widgets provided by Tk. These
include menus, scrollbars, and list boxes. This section gives you an overview of
some of the other Tk widgets and gives short examples explaining how these widgets
can be used.
<H4 ALIGN="CENTER"><A NAME="Heading22<FONT COLOR="#000077">Frames</FONT></H4>
<P>Frame widgets are containers for other widgets. They do not have any interesting
behavior like the other Tk widgets. The only visible properties of frame widgets
that you can set are their color and their border appearance. You can give three
different border appearances to a frame widget: flat, raised, and sunken. You can
experiment with the different frame widgets to see how they look.</P>
<P>The flat border frame widget is not too exciting. It looks exactly the same as
the default <TT>wish</TT> window (because the default border appearance is flat).
<H4 ALIGN="CENTER"><A NAME="Heading23<FONT COLOR="#000077">Buttons</FONT></H4>
<P>Button widgets are used to get specific input from a user. A button can be turned
on or activated by the user of a Tk program by moving the mouse pointer over the
button and then pressing the left mouse button. Tk provides the following three kinds
of button widgets:
<UL>
<LI>Button
<P>
<LI>Check button
<P>
<LI>Radio button
</UL>
<P>The button widget is used to initiate some specific actions. The button usually
has a name such as "Load file" that describes the action that results if
you press the button.</P>
<P>Check button widgets are used to allow users of a program to turn program options
on or off. When the check button is shaded the program option is on, and when the
check button is not shaded the program option is off.</P>
<P>Radio buttons are similar to check buttons except that they are defined in groups,
where only one member of a group of radio buttons is allowed to be on at one time.
This means that if one radio button in a group of radio buttons is on, none of the
other radio buttons in that group can be turned on. When the radio button is shaded
it is on, and when the radio button is not shaded it is off.
<H4 ALIGN="CENTER"><A NAME="Heading24<FONT COLOR="#000077">Menus and Menu Buttons</FONT></H4>
<P>Menu widgets are used to implement pull-down menus, cascading menus, and pop-up
menus. A menu is a top-level widget that contains a set of menu entries that have
values or commands associated with them. Five kinds of entries can be used in menus:
<UL>
<LI>Cascade entries display a submenu when the mouse pointer passes over them. The
cascade entry is similar in function to the menu button widget.
<P>
<LI>Command entries invoke a Tcl script when activated. The command entry is similar
to the button widget in function.
<P>
<LI>Check button entries toggle a program option between on and off. When the check
button is shaded the option is on, and when the check button is not shaded it is
off. The check button entry is similar in function to the check button widget.
<P>
<LI>Radio button entries toggle a program option. The difference between the radio
button entry and the check button entry is that radio buttons are typically defined
in groups, with the restriction that only one of the radio buttons in the group can
be active at once. The radio button entry is similar in function to the radio button
widget.
<P>
<LI>Separator entries display a horizontal line in the menu. This is used for appearance
purposes only. There is no behavior associated with a separator entry.
</UL>
<P>The main difference between the menu entries and the button widgets is that the
button widgets can exist by themselves, but the menu entries must exist within the
context of a menu widget.</P>
<P>Menu button widgets are similar to button widgets. The only real difference between
the two is that when menu buttons are invoked they bring up menus instead of executing
Tcl scripts as button widgets would. The menu button name usually describes the types
of menu entries contained in the menu that the menu button activates. This means
that you should find menu entries that perform some kind of file operations contained
within the File menu.</P>
<P>You can activate a menu by moving the mouse pointer to the menu button widget
and pressing the left mouse button. This activates the menu associated with the menu
button and displays the menu entries that are contained in that menu to the screen.
You can now move the mouse pointer down through the list of menu entries and select
the one you want.</P>
<P>The File menu contains two command entries (the Open entry and Quit entry), one
cascade entry (the Save As entry), and one separator entry. The menu that comes up
as a result of clicking the mouse pointer on the Save As cascade entry contains three
command entries: the Text entry, the Ver 1 file entry, and the Ver 2 file entry.
<H4 ALIGN="CENTER"><A NAME="Heading25<FONT COLOR="#000077">List Boxes</FONT></H4>
<P>The list box widget enables users of a Tk application to select items from a list
of one or more items. If the number of items to be displayed in the list box is larger
than the number of lines in the list box, you can attach scrollbars to make the extra
items accessible.
<H4 ALIGN="CENTER"><A NAME="Heading26<FONT COLOR="#000077">Scrollbars</FONT></H4>
<P>Scrollbar widgets are used to control what is displayed in other widgets. Scrollbar
widgets are attached to other widgets to allow users to scroll up and down through
the information contained in the widget. You typically put scrollbars on any widget
that is designed to contain an arbitrary number of lines of information (such as
a list box) or on widgets that contain more lines of information than the widget
can display, given its size.
<H3 ALIGN="CENTER"><A NAME="Heading27<FONT COLOR="#000077">Summary</FONT></H3>
<P>This chapter started off by introducing Tcl and the Tk tool kit and describing
the uses of both. Although this chapter contained a lot of information, it barely
scratched the surface of the programming tools provided by Tcl and the Tk tool kit.</P>
<P>Tcl has many more programming features than were described in this book. Some
of the most notable are arrays, lists, and procedures.</P>
<P>Not all of the Tk widgets were described here, either. Some of the widgets that
were not described are canvasses, scales, labels, messages, and <TT>textedit</TT>
widgets.</P>
<P>Tk is just one example of an extension to Tcl. There are many other extensions
available that extend the behavior of Tcl in different ways. Some of these extensions
are listed here:
<UL>
<LI>Ak: An audio extension for Tcl, Ak provides numerous commands for sound recording
and playback.
<P>
<LI>XF: This is an interactive graphical user interface developer for Tk.
<P>
<LI>Tcl-DP: This is a Tcl extension that helps programmers develop distributed applications
using Tcl.
</UL>
<P>If you would like to learn more about Tcl and Tk, a good place to start is the
manual pages for Tcl, Tk, and any of the specific commands you want information about.
There are also a few books available that are devoted to Tcl and Tk programming.
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -