526-529.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 124 行
HTML
124 行
<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=526-529//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="523-526.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="529-532.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The <TT>button</TT> command created a button widget that we called <TT>.b</TT>. To make the button appear in the <TT>wish</TT> window, we must tell Tk to display the button. This is done by the <TT>pack</TT> command.</P>
<P>In this example, the <TT>pack</TT> command has only one argument: the name of the button created in the first command. When the <TT>pack</TT> command is executed, a button with the string <TT>Hello there</TT> displayed in it appears in the <TT>wish</TT> window.</P>
<P>Two things about this example are worth discussing in more detail. The first is why we call the button <TT>.b</TT> instead of <TT>b</TT>, <TT>bob</TT>, or <TT>button1</TT>. The significance is not the actual text in the button name (this could, in fact, be <TT>bob</TT> or <TT>button1</TT>) but the period (<TT>.</TT>) preceding the name of the button.</P>
<P>The period notation is used to represent the widget hierarchy. Each widget is contained in another widget. The root widget, or the highest level widget, is contained in the <TT>wish</TT> window and is called <TT>.</TT> (this is analogous to the Linux directory structure, in which each directory has an owner or a parent directory and the root or highest level directory is named <TT>/</TT>). Each time a new widget is created, we must tell Tk which widget the new widget should be contained in. In the “Hello there” example, the container specified for the button widget was <TT>.</TT>, the root widget.</P>
<P>The second item of interest is the resizing of the <TT>wish</TT> window that occurs after entering the <TT>pack</TT> command. The <TT>wish</TT> window shrinks to a size that is just large enough to hold the button we’ve created. Tk causes the <TT>wish</TT> window to default to a size just large enough to hold whatever it has in it. Many commands can be used to change this behavior and customize how things are displayed on the screen. You will see some of these commands later in this chapter.</P>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">The Tcl Language</FONT></H3>
<P>Now that you have seen examples of both Tcl and Tk in action, it is appropriate to take a step back and look at the underlying Tcl language in more detail. Tcl contains a rich set of programming commands that supports all the features found in most high-level languages. This section discusses many of these features and gives examples that explain how to use them.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Tcl Variables and Variable Substitution</FONT></H4>
<P>Like the UNIX shell programming languages, Tcl supports the concept of variables. Variables are temporary storage places used to hold information that will be needed by a program at some later point in time. In Tcl, variable names can consist of any combination of printable characters.
</P>
<P>Typically, variable names are meaningful names that describe the information being stored in them. For example, a variable that is being used to hold the monthly sales of a product might have one of the following names:</P>
<!-- CODE SNIP //-->
<PRE>
Monthly_sales
“Monthly sales”
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>Quotation marks cause Tcl to ignore the whitespace characters (spaces and tabs) in the variable name and treat it as one word. This is discussed in the “Quotes” section of this chapter.<HR></FONT>
</BLOCKQUOTE>
<P>The value that is placed into a variable can also be any combination of printable characters. Possible values for the <TT>Monthly_sales</TT> variable are</P>
<!-- CODE SNIP //-->
<PRE>
“40,000”
40000
“refer to table 3”
</PRE>
<!-- END CODE SNIP //-->
<P>The Tcl <TT>set</TT> command is used to assign values to variables. The <TT>set</TT> command can be passed either one or two arguments. When two arguments are passed to the <TT>set</TT> command, the first one is treated as the variable name and the second is the value to assign to that variable.</P>
<P>When the <TT>set</TT> command is used with only one argument, Tcl expects the argument to be the name of a variable, and the <TT>set</TT> command returns the value of that variable. The following command assigns the value of 40000 to the variable <TT>Monthlysales</TT> and then echoes the value to the screen:</P>
<!-- CODE SNIP //-->
<PRE>
set Monthlysales 40000
</PRE>
<!-- END CODE SNIP //-->
<P>To print the value of the <TT>Monthlysales</TT> variable to the screen, type</P>
<!-- CODE SNIP //-->
<PRE>
set Monthlysales
</PRE>
<!-- END CODE SNIP //-->
<P>All values that are assigned to variables are stored as character strings. If we define a variable to be equal to the integer 40, as in the following command
</P>
<!-- CODE SNIP //-->
<PRE>
set num 40
</PRE>
<!-- END CODE SNIP //-->
<P>the value 40 is represented as the character string <TT>40</TT>, not as an integer.</P>
<P>So far you have seen how to set variables and how to display their values to the screen, but not how they are used with commands other than the <TT>set</TT> command. To use the value of a variable in another command, we must precede the variable name with an unquoted dollar sign ($). This tells Tcl to expect a variable name and to substitute the value of that variable for the variable name. The following example shows a simple use of variable substitution:</P>
<!-- CODE SNIP //-->
<PRE>
set Monthlysales 40000
expr $Monthlysales * 12
</PRE>
<!-- END CODE SNIP //-->
<P>The first command assigns the value of 40000 to the variable <TT>Monthlysales</TT>. The <TT>expr</TT> command is used to perform mathematical evaluations of expressions. In this case it takes the value of the variable <TT>Monthlysales</TT> and multiplies it by 12.</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Tcl Command Substitution</FONT></H4>
<P>Command substitution provides a way of substituting the result of a Tcl command (or commands) into an argument of another command. The syntax for command substitution is to include the commands that are being substituted into square brackets, as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
set Monthlysales 40000
set Yearlyforecast [ expr Monthlysales * 12 ]
</PRE>
<!-- END CODE SNIP //-->
<P>The first command once again sets the variable <TT>Monthlysales</TT> to the value of 40000. The second command makes use of command substitution to set the value of <TT>Yearlyforecast</TT> equal to the result of the command in the square braces.</P>
<P>In this example the substitute consists of only one command. Tcl allows the substitute to consist of any valid Tcl script, meaning that it can contain any number of Tcl commands.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="523-526.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="529-532.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?