-->

Previous | Table of Contents | Next

Page 499

CHAPTER 25

tcl and tk
Programming

by Sriranga R. Veeraraghavan

IN THIS CHAPTER

Page 500

The tcl (pronounced "tickle") scripting language and the tk toolkit are programming environments for creating graphical user interfaces for X Window System. tcl and tk are easy to learn and use, and with them, you can construct user interfaces much faster than with traditional X Window programming methods.

tcl/tk was written by John K. Ousterhout while he was a professor of electrical engineering and computer science at the University of California, Berkeley. It was originally designed to provide a reusable command language for interactive tools, but it has expanded far beyond that and is used in a wide range of software products.

The true power of tcl/tk is that complex graphical applications can be written almost entirely in the tcl scripting language, thus hiding many of the complexities of interface programming encountered in writing interfaces using the C language.

The official tcl/tk Web site is located at http://www.sunscript.com/.

This Web site offers information on releases, bug fixes, and ports along with HTML versions of the manual pages. The site also has links for downloading and installing the latest versions of tcl/tk. Presently, the newest available version of tcl is 7.6p2, and the newest available version of tk is 4.2.

The programs discussed in this chapter are compatible with most versions of tcl and tk.

tcl Basics

tcl is an interpreted language similar to the UNIX shell, which means that tcl commands are first read and then evaluated. tk is a windowing toolkit that uses the tcl syntax for creating GUI components such as buttons, scrollbars, dialogs, and windows.

In order to run tcl, the tcl shell (tclsh) or the windowing shell (wish) is required. Both tclsh and wish are similar to standard UNIX shells like sh or csh, in that they allow commands to be executed interactively or read in from a file. In practice, these shells are seldom used interactively because their interactive abilities are quite limited.

The main difference between tclsh and wish is that tclsh only understands tcl commands, while wish understands both tcl and tk commands.

Interactive Use of tcl

This section briefly covers the interactive use of the tcl shells to illustrate one of its hazards.

To start using tcl interactively, just type tclsh (or wish) at the UNIX shell's prompt. The following prompt should appear:


%

In this chapter, interactive commands start with the percent character (%). At the prompt, type


% echo "hello world"

Page 501

The words hello world should appear followed by a new prompt. Now try


% puts "hello world"

The same output should appear, but there is a big difference between the two. The first command ran the echo binary in order to echo the string "hello world", whereas the second command uses the puts (put string) tcl command. The echo version of "hello world" works only when tclsh is run interactively, which is one of the hazards of using tclsh and wish interactively. For example, if you put the command


echo "hello world"

into the file helloworld.tcl and then sourced that file from tclsh, as in


% source helloworld.tcl

you would get the following error:


invalid command name "echo"

To properly use UNIX commands in tcl, use the tcl command exec:


% exec echo "hello world"

This executes the command with its arguments in a UNIX shell. This is only one example of things that work differently in the interactive mode of the tcl shells.

Noninteractive Use of tcl

Commonly, tclsh and wish are used noninteractively, which means that they are invoked on scripts from the UNIX prompt ($) such as


$ tclsh myprog.tcl

$ wish myprog.tcl

or are called from within a script that has as its first line something like the following:


#!/usr/bin/wish

Usually this first line must be changed for each installation of the script because wish or tclsh will be in different places. In order to avoid having someone edit the script for each installation, the man page for tclsh recommends that the following three lines be used as the first three lines of all tcl/tk scripts:


#!/bin/sh

# the next line restarts using wish \

exec wish "$0" "$@"

This means that users only need to have wish in their path to use the script. Individual results with this approach could vary depending on the version of sh on the system.

The real advantage of noninteractive use of tcl is the same as for noninteractive use of the UNIX shell. Noninteractive use allows for many commands to be grouped together and executed by

Page 502

simply typing the name of the script and allows for faster development/debugging of large programs.

The tcl Language

This section contains an introduction to the tcl language syntax and its use in scripts. The code in the following section can be run interactively or from a script. The spacing of the output will vary slightly in interactive mode.

Command Structure

The basic structure of a tcl command is


commandname arguments

where commandname is the command that tcl is to execute, and arguments is the optional arguments to give to that command. The entire line (commandname and arguments) is called the command. Commands are separated by newlines (\n) or by a semicolon (;). If only one command is given on a line, the semicolon is not required. As an illustration, the two commands


set foo 0

set bar 1

can be written one per line or on the same line:


set foo 0; set bar 1;

Comments

Other than commands, the only other type of lines in a tcl script are comments. As in UNIX shells and Perl, a comment line is a line that begins with a pound symbol (#):


# this is a comment

but unlike in shell, the following is not a comment


set foo 0 # initialize foo

and will result in an error. The reason is that the tcl parser thinks that a command is terminated either by a newline or semicolon, so to include comments on the same line as a command, the command needs to be terminated by a semicolon:


set foo 0;# initialize foo

Thus, it is probably a good idea to terminate all commands with a semicolon, although it is not required.

Datatypes

tcl doesn't support variable types such as int, float, double, or char. This means that a variable can be set to a number, a character, or a string at different times in the same program.

Previous | Table of Contents | Next