Linux Unleashed, Third Edition:Introduction to Tcl and Tk
-->
Previous
Table of Contents
Next
Chapter 29Introduction to Tcl and Tk
by Tim Parker
In This Chapter
What is Tcl
What is Tk?
The Tcl language
The Tk language extensions
What Is Tcl?
Tcl stands for Tool Command Language. (It is pronounced tickle.) It is a scripting language similar to the shell scripting languages introduced in Chapter 14, Shell Programming. Tcl can be used to quickly write text-based application programs.
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 doesnt have to wait for code to compile and can see any changes made to the code almost instantly.
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.
Invoking Tcl commands is done by starting up the Tcl shell, called tclsh (or on some systems, tcl). Once tclshis started, we can enter Tcl commands directly into it. Straying slightly from the hello world example found in almost every introductory language text, the following example shows instead how to write Hello there to the screen:
puts stdout Hello there
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.
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.
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:
command parameter1 parameter2
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.
What Is Tk?
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).
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.
The Tk command shell is called wish, which stands for windowing shell. You must be running an X Window application when you invoke wish. This is because when wish is invoked, it brings up a window that displays the results of any of the graphical commands it interprets.
Now that we have seen what the Tk environment looks like, lets try enhancing the earlier Hello there example by displaying Hello there in a button in the wish window. To accomplish this, we must first ensure that wish has been started. This is easily done by typing the following command into an Xterm window:
wish
This command brings up the wish 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 Hello there in a button in the wish window are as follows:
button .b -text Hello there. -command exit
pack .b
Notice how the syntax of the command on the first line is essentially the same as the syntax of the puts 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 button command are slightly different from the arguments shown 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.
The second argument has the name text, and the value of the argument is the string we want to display in the button. The third argument has the name command and is used to specify the command that we want to execute when that button is pushed. In this example, we dont want anything to happen if the button is pushed, so we simply tell wish to exit from the current script.
Previous
Table of Contents
Next
Wyszukiwarka
Podobne podstrony:
2011 02 21 WIL Wyklad 19id 523521 523526 529519 52314 (526)523,37,artykul526 (2)więcej podobnych podstron