AGOCG logo
Graphics Multimedia VR Visualisation Contents
Training Reports Workshops Briefings Index

Graphical User Interfaces with Tcl/Tk


Building a graphical user interface to application programs written in FORTRAN or C is quite an involved and time consuming task. There have been many commercial and public domain products in recent years to make this activity more manageable. In spite of the best of efforts, some of these software systems involve very steep learning curves, and thus have remained in the domain of a select few. In the context of teaching the rudiments of GUIs, most of these systems involve a considerable amount of preparation both from the point of the teacher and the student. Also the available time in academic calendar poses another severe constraint.

The software developed by John Ousterhout in the early 80s provides an elegant solution to most of the above problems. It is called Tool Command Language (Tcl, pronounced as "tickle") and one of its most useful extensions called, Tool Kit. Tcl is a command language very akin to shell languages on Unix systems, and also a library. Tk provides various user interface objects which are reconfigurable interactively. The software is in the public domain (at under tcl) and has been ported to several operating systems (various versions of DOS, VMS, not windows yet!).

Tcl is a simple language whose primary function is to issue commands to interactive programs such as shells, text editors and debuggers. It is also programmable which enables the users to write more powerful commands that can be added to the built in set. Tcl is also a library package that can be embedded in application programs. Finally extensions such as the Tk toolkit, provide mechanisms for communicating between applications by sending Tcl commands back and forth. Tk extends the core Tcl facilities with commands for building user interfaces, so that the user can construct Motif like user interfaces by writing simple Tcl scripts instead of C code.

This article gives a fore taste of Tcl/Tk with an illustrated example and considers its main advantages as a teaching tool. For a more extensive list of applications the interested reader is referred to FAQ files. (These files are also stored at the ftp site: under/packages/tcl/tclarchive.

A Simple Example

The following example illustrates some of the basic features. Note that the example by no means encompasses all the functionality offered by Tcl/Tk, and is used here to merely indicate what is required in building GUIs.

The following commands can be stored in a file, say power.tcland sourced with wish}. Alternatively, these commands can be entered (and modified as necessary) interactively in the wish shell.

1 proc power {base p} {
2 set result 1
3 while {$p>0} {
4 set result [expr $result*$base]
5 set p [expr $p 1 ]
6 }
7 return $result
8 }
10 entry .base width 6 relief sunken
-textvariable base
11 label .label1 text "to the power"
12 entry .power width 6 relief sunken
-textvariable power
13 label .label2 text "is"
14 label .result -textvariable result
15 pack .base .label1 .power .label2 .result -side left \
16 -padx 1m -pady 2m
17 bind .base {set result [power $base $power] }
18 bind .power {set result [power $base $power] }

Figure A

Lines 1-9 define a procedure to compute $base^{power}$ and the languge constructs are somewhat reminiscent of C shell. Line 10 defines an entry widget whose name is .base. The dot preceding the name refers to the root window, and in general a widget in the hierarchy is refered to using the full path where the component names are separated by dot. Lines 10-14 define the required widgets configured with the few options as shown. Line 15-16 define the placement of the widgets, .base, .label1, .label2 and .result} in the root window. The packer takes care of enlarging or shrinking the root window as additional widgets are defined (or deleted). Finally the bind command in lines 17-18, binds the X events to Tcl commands. In this example, when the user presses the Return key either in the entry widget .base or in .power the procedure, power is invoked and the result displayed in .result}.

More sophisticated interfaces have been built using Tcl/Tk and this short article can not do full justice to its extensive capabilities. Currently, I am working on providing a simple GUI to example programs supplied with NAG Fortran 77/90 Library, and the preliminary designs have been extremely rewarding.

One can readily appreciate its potential as a powerful tool both for teaching GUIs to senior undergraduate students and postgraduate students; as well as for commercially viable products. One of main attractions of using Tcl/Tk for building GUIs is that the required programming is at a comfortable level, and provides a convenient transition path should the applications demand more specific functionality through the use of library routines and facilities to embed Tcl in other languages such as C, C++ and Ada.

The author can be reached at the address below and would like to have your comments in this regard.

Venkat V.S.S. Sastry
Applied Mathematics and Operational Research Group
Royal Military College of Science
Tel: +44 793 785315