Building GUIs with Tcl/Tk

For more details: ``Tcl and the Tk Toolkit'' by John Ousterhout (creator of Tcl/Tk)
Addison-Wesley, 1994, ISBN 0-201-63337-X

Tcl (Tool Command Language) and Tk (X11 Toolkit for use with Tcl) are convenient tools for building and debugging graphical user interfaces.


The easiest way to use Tcl is with a windowing shell called wishx. On our system, this program is
You can interactively build a GUI by running wishx, and then typing Tcl commands at the prompt. A window will open when wishx is run and as you type Tcl commands, the effects will be seen in the window. With Tcl you can define and place widgets like buttons, text entry fields, sliders, etc. in the GUI. Actions are attached to each widget, and the appropriate action will be executed with the widget is clicked, slid, etc.

There are other ways to use Tcl/Tk. For example, Tk routines can be invoked from C program you write. It is a X windows toolkit like Motif. However, the power of using wishx is the interactivity and ease/speed of debugging the interface code, which is traditionally complicated, tedious to write, and error prone.

Examples (try them!)

Here is a simple TCL SCRIPT that opens a window and provides a button for exiting the program. Not much to it, but it doesn't do much either.

Here is a more complicated TCL SCRIPT that opens a window, takes input from the keyboard, and computes a base raised to a power. To use the text entry fields, you must first click the left mouse button on the field; then it will take typing. The script has been written to allow CR, right mouse button click, or the "r" key to activate the computation... try it!

Finally, here is an interesting TCL SCRIPT for remembering and running Unix commands. It had a text entry field for extering and executing a command. It then adds a button to a menu, remembering the command and allowing the user to re-run the command by clicking on the button. It holds a max of 7 commands, then rolls the oldest off the end.

Running these scripts

You can execute one of these Tcl scripts by simply doing this: The first line of the script identifies wishx as the program that should interpret the script. Then, you will get the same effect as if you ran wishx directly and typed all those Tcl commands to the shell.

Source command

You can get the effect of a script from within wishx with the source command. From the wishx prompt, type "source tryme" and the script "tryme" will be read in as if you typed it all. The first line is a comment and will be ignore in this case.

History command

The history command will make a transcript of your session with wishx. This will make it easier to go from interactive GUI development and debugging to a Tcl script for production use.

XF: Graphical construction of GUIs

You might be interested in trying XF. This is a tool that will allow you to layout the widgets and functions of a GUI with a graphical palett, using the mouse. After arranging the widgets as you want them, XF will dump out a Tcl script.