?? rhl30.htm
字號(hào):
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl30.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl31.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl31.htm">
<LINK REL="Previous" HREF="rhl29.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl29.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl30.htm</H2>
<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="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>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E238" >What Is Tcl?</A>
<LI>
<A HREF="#E68E239" >What Is Tk?</A>
<LI>
<A HREF="#E68E240" >The Tcl Language</A>
<UL>
<LI>
<A HREF="#E69E364" >Tcl Variables and Variable Substitution</A>
<LI>
<A HREF="#E69E365" >Tcl Command Substitution</A>
<LI>
<A HREF="#E69E366" >Quotes</A>
<LI>
<A HREF="#E69E367" >The if Command</A>
<LI>
<A HREF="#E69E368" >The for Command</A>
<LI>
<A HREF="#E69E369" >The while Command</A>
<LI>
<A HREF="#E69E370" >The switch Command</A>
<LI>
<A HREF="#E69E371" >Comments</A></UL>
<LI>
<A HREF="#E68E241" >The Tk Language Extensions</A>
<UL>
<LI>
<A HREF="#E69E372" >Frames</A>
<LI>
<A HREF="#E69E373" >Buttons</A>
<LI>
<A HREF="#E69E374" >Menus and Menu Buttons</A>
<LI>
<A HREF="#E69E375" >List Boxes</A>
<LI>
<A HREF="#E69E376" >Scrollbars</A></UL>
<LI>
<A HREF="#E68E242" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E30"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>30</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E30"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Introduction to Tcl and Tk</B></FONT></CENTER></H2>
<BR>
<P>This chapter introduces the Tcl programming language and its most popular extension, Tk. This chapter covers the following topics:
<BR>
<UL>
<LI>What Tcl is
<BR>
<BR>
<LI>What Tk is
<BR>
<BR>
<LI>The Tcl language
<BR>
<BR>
<LI>The Tk language extensions
<BR>
<BR>
</UL>
<P>By the end of this chapter, you should understand what Tcl and Tk are, and also how to write simple applications using Tcl and Tk.
<BR>
<BR>
<A NAME="E68E238"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>What Is Tcl?</B></FONT></CENTER></H3>
<BR>
<P>Tcl stands for Tool Command Language. (It is pronounced "tickle.") It is a scripting language similar to the shell scripting languages introduced in <A HREF="rhl13.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl13.htm">Chapter 13</A>, "Shell Programming." Tcl can be used to quickly
write text-based application programs.
<BR>
<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 is usually much faster than using compiled languages. This is because you don't have to wait for code to
compile and can see any changes you make to the code almost instantly.
<BR>
<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.
<BR>
<P>Invoking Tcl commands is done by starting up the Tcl shell, called tclsh. Once you have started tclsh, you can type Tcl commands directly into it. Straying slightly from the "hello world" example that you find in almost every introductory
language text, the following example shows you how to write Hello there instead to the screen:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">puts stdout "Hello there"</FONT></PRE>
<P>This command contains three separate words. The first word in the command is the actual command name, puts. The puts 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 puts to write to the standard output device, which is typically the screen. The third word in the puts command is "Hello there". The quotation marks tell Tcl to interpret everything contained within them as a
single word.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The default output device is stdout (standard output, usually the screen), so if you intend to write something to stdout using the puts command, the second argument is optional.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></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:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">command parameter1 parameter2 ...</FONT></PRE>
<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.
<BR>
<BR>
<A NAME="E68E239"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>What Is Tk?</B></FONT></CENTER></H3>
<BR>
<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.
<BR>
<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 you can also do in the Tk command shell. The big
difference between the two is that the Tk command shell was designed to enable you to build X Window front ends to your applications.
<BR>
<P>The Tk command shell is called wish, which stands for windowing shell. You must be running X Window when you invoke wish. This is because when wish is invoked, it brings up a window to display the results of any of the graphical commands it interprets.
When you invoke wish, a window should appear on your screen.
<BR>
<P>Let's try enhancing the earlier "Hello there" example by displaying Hello there in a button in the wish window. To accomplish this, you must first ensure that wish has been started. This is easily done by typing the following command into an
Xterm window:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">wish</FONT></PRE>
<P>This command brings up the wish window and also executes the Tk interpreter in the Xterm window. You can now type Tcl or Tk commands directly in the Xterm window. The commands necessary to print Hello there in a button in the wish window are as follows:
<BR>
<PRE>
<FONT COLOR="#000080">button .b -text "Hello there" -command exit
pack .b</FONT></PRE>
<P>The syntax of the command on the first line contains the command name followed by a number of arguments. The first argument is the name you are giving to the new button. The rest of the arguments passed to the button command are slightly different from
the arguments you saw in the Tcl version of the "Hello there" 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.
<BR>
<P>The second argument has the name text, and the value of the argument is the string you want to display in the button. The third argument has the name command and is used to specify the command that you want to execute when that button is pushed. In this
example, you do not really want anything to happen if the button is pushed, so you just tell wish to exit from the current script.
<BR>
<P>The button command created a button widget that you called .b. To get the button to show up in the wish window, you must tell Tk to display the button. This is done by the pack command.
<BR>
<P>In this example, the pack command has only one argument: the name of the button that you created in the first command. When the pack command is executed, a button with the string Hello there displayed in it appears in the wish window.
<BR>
<P>Two things about this example are worth discussing in more detail. The first is why you called the button .b instead of b, bob, or button1. The significance is not the actual text in the button name (this in fact could be bob or button1), but the period
(.) preceding the name of the button.
<BR>
<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 wish window and is called . (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 /). Each time you create a new widget, you 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 ., the root widget.
<BR>
<P>The second item of interest is the resizing of the wish window that occurs after you enter the pack command. The wish window shrinks down to a size that is just large enough to hold the button you created. Tk causes the wish 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.
<BR>
<BR>
<A NAME="E68E240"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Tcl Language</B></FONT></CENTER></H3>
<BR>
<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 support 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.
<BR>
<BR>
<A NAME="E69E364"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Tcl Variables and Variable Substitution</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<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:
<BR>
<PRE>
<FONT COLOR="#000080">Monthly_sales
"Monthly sales"</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>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.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The value that is placed into a variable can also be any combination of printable characters. Possible values for the Monthly_sales variable are
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -