📄 rhl30.htm
字号:
<FONT COLOR="#000080">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>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The Tcl switch command is equivalent to the case statement found in Pascal and some other languages.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>This switch command compares the value that is stored in the thing variable (which must be set prior to these statements, of course) with the string car and the string truck to see if it matches either of them. If the value of the thing variable is
equal to car, then thing is a car is displayed on the screen. If the value of the thing variable is equal to truck, then thing is a truck is displayed on the screen. If neither of these cases are true, the default clause is executed and I don't know what
this thing is displays on the screen.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Whenever you need to check to see if a variable is equal to one of a number of values, you should use a switch command instead of an if command with multiple elseif clauses. This makes your code much easier to read and understand.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E371"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Comments</B></FONT></CENTER></H4>
<BR>
<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 are possible:
<BR>
<UL>
<LI>Someone else needs to look at or maintain your code.
<BR>
<BR>
<LI>Your programs get large.
<BR>
<BR>
<LI>You won't be looking at code that you have written for long periods of time after you write it.
<BR>
<BR>
</UL>
<P>Chances are that at least one of these situations will come up with Tcl code you have written.
<BR>
<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.
<BR>
<BR>
<PRE>
<FONT COLOR="#000080"># This is a valid comment
<BR>
set a 1 ; # This is a valid comment
<BR>
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.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Recall that Tcl interprets everything up to a newline character or a semicolon to be part of the command.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E241"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Tk Language Extensions</B></FONT></CENTER></H3>
<BR>
<P>Earlier in this chapter, a simple example of Tk displayed Hello there in a button in the wish 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.
<BR>
<BR>
<A NAME="E69E372"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Frames</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<P>The flat border frame widget is not too exciting. It looks exactly the same as the default wish window (because the default border appearance is flat).
<BR>
<BR>
<A NAME="E69E373"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Buttons</B></FONT></CENTER></H4>
<BR>
<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:
<BR>
<UL>
<LI>Button
<BR>
<BR>
<LI>Check button
<BR>
<BR>
<LI>Radio button
<BR>
<BR>
</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.
<BR>
<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.
<BR>
<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.
<BR>
<BR>
<A NAME="E69E374"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Menus and Menu Buttons</B></FONT></CENTER></H4>
<BR>
<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:
<BR>
<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.
<BR>
<BR>
<LI>Command entries invoke a Tcl script when activated. The command entry is similar to the button widget in function.
<BR>
<BR>
<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.
<BR>
<BR>
<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.
<BR>
<BR>
<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.
<BR>
<BR>
</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.
<BR>
<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.
<BR>
<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.
<BR>
<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.
<BR>
<BR>
<A NAME="E69E375"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>List Boxes</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<BR>
<A NAME="E69E376"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Scrollbars</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<BR>
<A NAME="E68E242"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>
<BR>
<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.
<BR>
<P>Tcl has many more programming features than were described in this book. Some of the most notable are arrays, lists, and procedures.
<BR>
<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 textedit widgets.
<BR>
<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 as follows:
<BR>
<UL>
<LI>Ak: An audio extension for Tcl, Ak provides numerous commands for sound recording and playback.
<BR>
<BR>
<LI>XF: This is an interactive graphical user interface developer for Tk.
<BR>
<BR>
<LI>Tcl-DP: This is a Tcl extension that helps programmers develop distributed applications using Tcl.
<BR>
<BR>
</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.<A
NAME="I2"></A>
<P ALIGN=LEFT>
<A HREF="rhl29.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl29.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl31.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl31.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -