Programming With Tcl And Tk

Tcl and Tk are available on CSE departmental machines. Tcl (an abbreviation for Tool Command Language, and sometimes pronounced "tickle") is a programming language and a set of library functions that interpret it, while Tk (for Tool Kit) is set of X Window System widgets with a Tcl interface. They were developed by John Ousterhout at Berkeley, and he is actively supporting them. They are located in /usr/local/lib/tcl and /usr/local/lib/tk.

The Structure of a Tcl Application

Together, Tcl and Tk help a programmer to create applications that are easy to customize and extend. In a typical Tcl application, the programmer writes a set of low-level functions in C or some other compiled language. These are linked together with the Tcl interpreter and the standard library of built-in Tcl and Tk functions.

When the program executes, it reads an initialization script written in the Tcl language that sets up the user interface and binds X events to Tcl scripts. Whenever the user pushes a button on the screen or types a character or does anything else that generates an X event, the corresponding script executes to carry out the requested action.

The script can be as simple as a call to a built-in or programmer-defined function, or it can include loops and conditionals and calls to Tcl-language procedures. In fact, the Tcl language and built-in functions are rich enough to allow the programmer to write the entire program in Tcl and Tk, without using any C code at all.

Some Programming Examples

The control structures in Tcl resemble those of C and of shell languages. All data is stored as strings of characters, and these strings may represent numeric values or lists, as well as text. While using strings to store data is not always efficient, it does allow for some interesting and powerful programming constructs.

Here is an example of a Tcl script that creates a list of chores and then does them in order:

set jobs { wash vacuum eat study }
foreach chore $jobs {
	echo ${chore}ing
	$chore
	set checklist($chore) done
}
The first line creates a variable called jobs and sets it to a list of four items. Next comes a foreach loop that sets the variable chore to each item in the jobs list successively. Note that the $ operator is used to access the contents of the variable; without it, jobs would be interpreted as a one-item list consisting of the word "jobs."

The curly brackets enclose a list of commands to be done for each chore in the list. In the first of these, the name of the task is printed with the suffix "ing." The curly brackets separate the name of the variable from the text of the suffix. When the program runs, it will print "washing," "vacuuming," etc.

Next, the line $chore causes the current contents of that variable to be called as a function. Note how the words in the list can be treated as either literal text or the name of an object.

The third line of the loop sets an element of the array checklist to contain the string "done." In this case, the contents of chore are used as an index into the array. Arrays in Tcl can be indexed by any string, and they do not need to be declared in advance. Assuming that the functions wash, vacuum, eat, and study exist, the code above is a complete and legal Tcl script.

Tcl's standard library includes functions for list processing, string formatting, regular expression handling, file and operating system access, and error handling.

X11 User Interface

Programming an X11 user interface with Tk is particularly simple. The following lines of code create a window with an editable field and close button:

set text "To be or not to be"
entry .hello_entry -textvariable text -relief sunken
button .done_button -text "Hasta la vista, baby" -command {
	echo "Text was: $text"
	echo "I'llbe back"
	destroy
}
pack append . .hello_entry {top} .done_button { top }

After setting a variable with the initial text, the script uses the entry command to create a widget (with a sunken appearance) linked to the variable text. Editing the field on screen alters the contents of the variable.

The next line creates a button with some text and a set of commands to be executed when it's pushed. The command can also be a call to a user-defined procedure containing more elaborate instructions.

Finally, the pack command arranges the widgets in the main window (called ".") so that the entry is at the top of the window and the button is at the top of the remaining space in the window. The sizes, colors, bindings, and other parameters of the widgets can all be customized.

Anyone who has written a significant amount of X11 code in C will realize how much more work it would take to create a similar display using one of the C-based widget sets. Tk includes a large number of widgets, including menus, scrollbars, editable text windows, and canvases.

A Tcl/Tk Shell

The Tcl/Tk distribution includes a shell called wish (for WIndowing SHell) that can execute scripts directly. You can try sample programs simply by starting wish and typing a script or reading it from a file with the source command, just as you would with the standard Unix shells. The source code for the wish program is a good place to start if you will be writing a larger Tcl application with your own C-language functions. It shows how to initialize a Tcl interpreter and add your own C-language commands.

Writing Tcl and Tk Programs

Since Tcl scripts can call both compiled C-language functions and functions written in the Tcl language, the user can choose to write nearly any function a program calls in either C or Tcl. Because Tcl is interpreted, Tcl-language functions can be edited and reexecuted without quitting the application that calls them. This makes for very fast testing and debugging. On the other hand, compiled functions will run faster, and in time-critical applications, it may be wise to write a function first in Tcl to get it working and then translate it to C once its operation and use are well-defined.

Programs based on Tcl and Tk can communicate with each other through the send command. This allows programs to cooperate and exchange information easily, which in turn allows integrated systems to be built from modular components. For example, Ousterhout has suggested that a programming environment could include a debugger and editor that cooperated with each other; the debugger would tell the editor which lines of source code to display, and the editor could tell the debugger where to set breakpoints.

Further Information

You can learn more about Tcl and Tk from a several sources. One place to start is a pair of papers that Ousterhout presented at the USENIX Winter meetings in 1990 and 1991. These papers and other related documents are available online by anonymous ftp from sprite.berkeley.edu.

The system itself includes a complete set of man pages ( man -k tcl and man -k tk ), as well as some on-line demonstration code in /usr/local/lib/tk/demos that can be run with wish. Another way to learn the system is to read the book that Ousterhout is writing about Tcl and Tk. It is scheduled to be published this year.

You can also get information from the newsgroup comp.lang.tcl. A list of frequently-asked questions is posted periodically, and it includes a description of several tools that use or extend Tcl and Tk.

John May -- CSE Graduate Student


Go to next article in this issue

Go to previous article in this issue

Go back to July-Aug., 1993 Table of Contents