523-526.html

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

HTML
110
字号
<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=523-526//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch28/521-522.html">Previous</A></TD>

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

<TD><A HREF="526-529.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 29<BR>Introduction to Tcl and Tk

</FONT></H2>

<P><I>by Tim Parker</I></P>

<DL>

<DT><B>In This Chapter</B>

<DT>&#149;&nbsp;&nbsp; What is Tcl

<DT>&#149;&nbsp;&nbsp; What is Tk?

<DT>&#149;&nbsp;&nbsp; The Tcl language

<DT>&#149;&nbsp;&nbsp; The Tk language extensions

</DL>

<H3><A NAME="Heading2"></A><FONT COLOR="#000077">What Is Tcl?</FONT></H3>

<P>Tcl stands for Tool Command Language. (It is pronounced &#147;tickle.&#148;) It is a scripting language similar to the shell scripting languages introduced in Chapter 14, &#147;Shell Programming.&#148; Tcl can be used to quickly write text-based application programs.

</P>

<P>Tcl was developed by John Ousterhout, then of the University of California at Berkeley. Tcl is an interpreted language and therefore has the usual advantages and disadvantages of all interpreted languages. The key disadvantage of interpreted languages is that they execute much slower than compiled languages. The biggest advantage of interpreted languages is that developing applications using them are usually much faster than using compiled languages. This is because the programmer doesn&#146;t have to wait for code to compile and can see any changes made to the code almost instantly.</P>

<P>Tcl has a core set of built-in functions that provide the basic features of its programming language. The true power of Tcl, however, is that it is easily extendible. Application programmers can add functions to Tcl and can even imbed the Tcl function library directly into their applications. This gives programmers the power to include an interpretive scripting language directly in their own application without having to do any of the work of developing the language. This means that you can provide users of your application with all the commands that exist within Tcl and also any that you create and add to the Tcl library.</P>

<P>Invoking Tcl commands is done by starting up the Tcl shell, called <TT>tclsh</TT> (or on some systems, <TT>tcl</TT>). Once <TT>tclsh</TT>is started, we can enter Tcl commands directly into it. Straying slightly from the &#147;hello world&#148; example found in almost every introductory language text, the following example shows instead how to write <TT>Hello there</TT> to the screen:</P>

<!-- CODE SNIP //-->

<PRE>

puts stdout &#147;Hello there&#148;

</PRE>

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

<P>This command contains three separate words. The first word in the command is the actual command name, <TT>puts</TT>. The <TT>puts</TT> command is an abbreviation for put string; it simply writes something to the device that is specified in the second word of the command. In this case the second word tells <TT>puts</TT> to write to the standard output device, which is typically the screen. The third word in the <TT>puts</TT> command is <TT>&#147;Hello there&#148;</TT>. The quotation marks tell Tcl to interpret everything contained within them as a single word.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>The default output device is <TT>stdout</TT> (standard output, usually the screen), so if you intend to write something to <TT>stdout</TT> using the <TT>puts</TT> command, the second argument is optional.

<HR></FONT>

</BLOCKQUOTE>

<P>Even though this is a very simple example, it illustrates the basic command syntax of the Tcl language. There are obviously many more commands contained within Tcl, but the basic syntax of all Tcl commands is the same:

</P>

<!-- CODE SNIP //-->

<PRE>

command parameter1 parameter2 &#133;

</PRE>

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

<P>The command can be any of the built-in Tcl commands, or it can be a user-defined extension to the Tcl command set in the form of a Tcl procedure. In either case, the fundamental syntax remains unchanged.

</P>

<H3><A NAME="Heading3"></A><FONT COLOR="#000077">What Is Tk?</FONT></H3>

<P>Tk, which was also developed by Ousterhout, is a graphical user interface extension to Tcl. Tk is based on the X Window system and allows application developers to develop X Window-based applications much faster than they could using other X Window toolkits, such as Motif or Open Look. In many ways, you can think of Tk as similar to the Windows-based Visual language series (Visual Basic, for example).

</P>

<P>Like Tcl, Tk also has a shell that enables you to enter commands to be interpreted. The Tk shell is a superset of the Tcl command shell. This means that anything you can do in the Tcl command shell can also be done in the Tk command shell. The big difference between the two is that the Tk command shell is designed to enable you to build X Window front ends to your applications.</P>

<P>The Tk command shell is called <TT>wish</TT>, which stands for windowing shell. You must be running an X Window application when you invoke <TT>wish</TT>. This is because when <TT>wish</TT> is invoked, it brings up a window that displays the results of any of the graphical commands it interprets.</P>

<P>Now that we have seen what the Tk environment looks like, let&#146;s try enhancing the earlier &#147;Hello there&#148; example by displaying <TT>Hello there</TT> in a button in the <TT>wish</TT> window. To accomplish this, we must first ensure that <TT>wish</TT> has been started. This is easily done by typing the following command into an Xterm window:</P>

<!-- CODE SNIP //-->

<PRE>

wish

</PRE>

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

<P>This command brings up the <TT>wish</TT> window and also executes the Tk interpreter in the Xterm window. We can now type Tcl or Tk commands directly in the Xterm window. The commands necessary to print <TT>Hello there</TT> in a button in the <TT>wish</TT> window are as follows:</P>

<!-- CODE SNIP //-->

<PRE>

button .b -text &#147;Hello there.&#148; -command exit

pack .b

</PRE>

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

<P>Notice how the syntax of the command on the first line is essentially the same as the syntax of the <TT>puts</TT> command. It contains the command name followed by a number of arguments. The first argument is the name we are giving to the new button. The rest of the arguments passed to the <TT>button</TT> command are slightly different from the arguments shown in the Tcl version of the &#147;Hello there&#148; example. These arguments each consist of two parts. The first part tells Tk what the argument name is, and the second part tells Tk the value of the argument.</P>

<P>The second argument has the name <TT>text</TT>, and the value of the argument is the string we want to display in the button. The third argument has the name <TT>command</TT> and is used to specify the command that we want to execute when that button is pushed. In this example, we don&#146;t want anything to happen if the button is pushed, so we simply tell <TT>wish</TT> to exit from the current script.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch28/521-522.html">Previous</A></TD>

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

<TD><A HREF="526-529.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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