532-535.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 122 行

HTML
122
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Introduction to Tcl and Tk</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=29//-->

<!--PAGES=532-535//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="529-532.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="535-536.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">The while Command</FONT></H4>

<P>The <TT>while</TT> command is used to implement <TT>while</TT> loops in Tcl. <TT>while</TT> loops are very similar to <TT>for</TT> loops. The only real difference between them is that the <TT>for</TT> loop provides more enhanced features for controlling entrance and exit criteria for the loop. The syntax for the <TT>while</TT> loop is shown in the following example:</P>

<!-- CODE SNIP //-->

<PRE>

set I 0

while &#123;&#36;i &lt; 10&#125; &#123;

   puts [expr 2 * &#36;i]

   set i [expr &#36;i &#43; 1]

&#125;

</PRE>

<!-- END CODE SNIP //-->

<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 <TT>2 * 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.</P>

<H4 ALIGN="LEFT"><A NAME="Heading11"></A><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.</P>

<!-- CODE SNIP //-->

<PRE>

switch &#36;thing &#123;

   car &#123;puts &#147;thing is a car&#148;&#125;

   truck &#123;puts &#147;thing is a truck&#148;&#125;

   default &#123;puts &#147;I don&#146;t know what this thing is&#148;&#125;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The Tcl <TT>switch</TT> command is equivalent to the <TT>case</TT> statement found in Pascal and some other languages. 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 is true, the default clause is executed and <TT>I don&#146;t know what this thing is</TT> displays on the screen.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>Whenever you need to check to see if a variable is equal to one of a number of values, use a <TT>switch</TT> command instead of an <TT>if</TT> command with multiple <TT>elseif</TT> clauses. This makes the code much easier to read and understand.<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Comments</FONT></H4>

<P>It is always a good idea to include comments in any Tcl code you write&#151;or code you write in any other language, for that matter. This becomes especially important if any of the following situations are possible:

</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;Someone else needs to look at or maintain your code.

<DD><B>&#149;</B>&nbsp;&nbsp;Your programs get large.

<DD><B>&#149;</B>&nbsp;&nbsp;You won&#146;t be looking at code that you have written for long periods of time after you write it.

</DL>

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

<!-- CODE SNIP //-->

<PRE>

# This is a valid comment



set a 1 ;  # This is a valid comment



set a 1      # This is an invalid comment

</PRE>

<!-- END CODE SNIP //-->

<P>The third comment shown here is invalid because it occurs in the middle of a command. Remember that Tcl interprets everything up to a newline character or a semicolon to be part of the command.

</P>

<H3><A NAME="Heading13"></A><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, as well as short examples explaining how these widgets can be used.</P>

<H4 ALIGN="LEFT"><A NAME="Heading14"></A><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 the color and 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 especially interesting. It looks exactly the same as the default <TT>wish</TT> window (because the default border appearance is flat).</P>

<H4 ALIGN="LEFT"><A NAME="Heading15"></A><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:

</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;Button

<DD><B>&#149;</B>&nbsp;&nbsp;Check button

<DD><B>&#149;</B>&nbsp;&nbsp;Radio button

</DL>

<P>The button widget is used to initiate some specific actions. The button usually has a name such as &#147;Load file&#148; 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.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="529-532.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="535-536.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?