⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch30.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 3 页
字号:


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

 -->




 



<UL>



	<LI><A HREF="#Heading1">- 30 -</A>



	<UL>



		<LI><A HREF="#Heading2">Tcl and Tk</A>



		<UL>



			<LI><A HREF="#Heading3">What Is Tcl?</A>



			<LI><A HREF="#Heading4">NOTE</A>



			<LI><A HREF="#Heading5">What Is Tk?</A>



			<LI><A HREF="#Heading6">The Tcl Language</A>



			<UL>



				<LI><A HREF="#Heading7">Tcl Variables and Variable Substitution</A>



			</UL>



			<LI><A HREF="#Heading8">NOTE</A>



			<UL>



				<LI><A HREF="#Heading9">Tcl Command Substitution</A>



				<LI><A HREF="#Heading10">Quotes</A>



				<LI><A HREF="#Heading11">The if Command</A>



			</UL>



			<LI><A HREF="#Heading12">NOTE</A>



			<LI><A HREF="#Heading13">WARNING</A>



			<UL>



				<LI><A HREF="#Heading14">The for Command</A>



				<LI><A HREF="#Heading15">The while Command</A>



				<LI><A HREF="#Heading16">The switch Command</A>



			</UL>



			<LI><A HREF="#Heading17">NOTE</A>



			<LI><A HREF="#Heading18">TIP</A>



			<UL>



				<LI><A HREF="#Heading19">Comments</A>



			</UL>



			<LI><A HREF="#Heading20">NOTE</A>



			<LI><A HREF="#Heading21">The Tk Language Extensions</A>



			<UL>



				<LI><A HREF="#Heading22">Frames</A>



				<LI><A HREF="#Heading23">Buttons</A>



				<LI><A HREF="#Heading24">Menus and Menu Buttons</A>



				<LI><A HREF="#Heading25">List Boxes</A>



				<LI><A HREF="#Heading26">Scrollbars</A>



			</UL>



			<LI><A HREF="#Heading27">Summary</A>



		</UL>



	</UL>



</UL>







<P>



<HR SIZE="4">







<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 30 -</FONT></H2>



<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Tcl and Tk</FONT></H2>



<P><I>by Rick McMullin</I></P>



<P>IN THIS CHAPTER</P>







<UL>



	<LI>What Is Tcl? 



	<P>



	<LI>What Is Tk? 



	<P>



	<LI>The Tcl Language 



	<P>



	<LI>The Tk Language Extensions 



</UL>







<P>This chapter introduces the Tcl programming language and its most popular extension,



Tk. This chapter covers the following topics:







<UL>



	<LI>What Tcl is



	<P>



	<LI>What Tk is



	<P>



	<LI>The Tcl language



	<P>



	<LI>The Tk language extensions



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



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



<P>Tcl stands for Tool Command Language. (It is pronounced &quot;tickle.&quot;) It



is a scripting language similar to the shell scripting languages introduced in Chapter



13, &quot;Shell Programming.&quot; 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 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.</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 you have started <TT>tclsh</TT>, you can



type Tcl commands directly into it. Straying slightly from the &quot;hello world&quot;



example that you find in almost every introductory language text, the following example



shows you how to write <TT>Hello there</TT> instead to the screen:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">puts stdout &quot;Hello there&quot;



</FONT></PRE>



<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>&quot;Hello there&quot;</TT>. The quotation marks tell



Tcl to interpret everything contained within them as a single word.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading4<FONT COLOR="#000077"><B>NOTE:</B> </FONT>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>







</DL>







<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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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.



<H3 ALIGN="CENTER"><A NAME="Heading5<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.</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 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.</P>



<P>The Tk command shell is called <TT>wish</TT>, which stands for windowing shell.



You must be running X Window when you invoke <TT>wish</TT>. This is because when



<TT>wish</TT> is invoked, it brings up a window to display the results of any of



the graphical commands it interprets. When you invoke <TT>wish</TT>, a window should



appear on your screen.</P>



<P>Now that you have seen what the Tk environment looks like, let's try enhancing



the earlier &quot;Hello there&quot; example by displaying <TT>Hello there</TT> in



a button in the <TT>wish</TT> window. To accomplish this, you must first ensure that



<TT>wish</TT> has been started. This is easily done by typing the following command



into an Xterm window:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">wish



</FONT></PRE>



<P>This command brings up the <TT>wish</TT> window and also executes the Tk interpreter



in the Xterm window. You can now type Tcl or Tk commands directly in the Xterm window.



These are the commands necessary to print <TT>Hello there</TT> in a button in the



<TT>wish</TT> window:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">button .b -text &quot;Hello there.&quot; -command exit



pack .b



</FONT></PRE>



<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 you are giving to the new



button. The rest of the arguments passed to the <TT>button</TT> command are slightly



different from the arguments you saw in the Tcl version of the &quot;Hello there&quot;



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 you want to display in the button. The third argument has the name



<TT>command</TT> 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 <TT>wish</TT> to exit from the current



script.</P>



<P>The <TT>button</TT> command created a button widget that you called <TT>.b</TT>.



To get the button to show up in the <TT>wish</TT> window, you 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 that you 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 you called 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 in fact could 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 you create a



new widget, you must tell Tk which widget the new widget should be contained in.



In the &quot;Hello there&quot; 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 you enter the <TT>pack</TT> command. The <TT>wish</TT> window shrinks down



to a size that is just large enough to hold the button you 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.



<H3 ALIGN="CENTER"><A NAME="Heading6<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 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.



<H4 ALIGN="CENTER"><A NAME="Heading7<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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">Monthly_sales



&quot;Monthly sales&quot;



</FONT></PRE>







<DL>



⌨️ 快捷键说明

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