Computer Languages from A to Z: Tcl/Tk

by Gary L. Ratliff Sr. (eronstuc)

This month, we take a look at Tcl/Tk. We learn from the excellent book, Graphical Applications with Tcl & Tk, Second Edition, by Eric Foster Johnson, that Tcl/Tk is often pre-installed with Linux.

To pursue this idea, I queried the versions of Linux on my main computer to learn if this were true, and if so, which version was installed: PCLinux 2010 tk 8.6, PCLinux 2009-1 tk 8.6, PCLinux 2007 tk 8.4, Debian 2.5-26-686 not installed, Debian 2.6.26 amd 64 not installed, openSUSE 11.2 tk 8.5, Ubuntu 9.10 tk 8.4, Ubuntu 10.04 LTS tk 8.4, Mythbuntu 8.04 LTS tk 8.4, Dreamlinux 3.5 tk 8.4, Absolute Linux (Slackware 13.0.0.0.0) tk 8.5, KNOPPIX 6.2 tk 8.4, Linux Mint 8 (Helena) tk 8.4, and Mandrivia 2010 tk 8.6.

Tcl (tool command language) was created by John Ousterhout during the late 1980's. He first developed the idea in 1987 as he was working in the design of tools. He began giving the system away, and later became concerned with the complexities of developing GUI',s which idea resulted in Tk, or a graphical toolkit.

Make a Wish

The Tcl/Tk set is comprised of two interpreters. One, tclsh, is a shell language like bash (or the korn shell mentioned a few months ago), which only understands tcl. The other, which allows the graphical features of tk, which is known as wish. Often this will include the version of the tk used as wish80, which is the version covered by the Eric Foster Johnson book. To illustrate just how easy a graphical may be created. The ubiquitous "Hello World" program is presented in tk as follows:

button .a -text "Hello World!" -command { exit} ; pack .a

These two lines entered into the console, which appears after you issue the wish command, will soon create a button which displays the message "Hello World!," and which closes when you click on it. From this humble introduction you can begin to learn the details of creating and using the various widgets offered in the tk system.

The book was written about ten years after Ousterhout first developed the idea of Tcl. And in 800 plus pages the details of using buttons, dialog windows, canvas widgets and the rest of the items available are explained and illustrated. The book also included a CD with all the code and the 8.0 version of wish and a much earlier version on the CD.

Early in the week, my daughter arrived from a visit to Nashville, and showed me that my brother had sent me his old IBM Aptiva computer. This computer had Windows 95 installed, and soon version 8.0 of wish and version 8.0 0f tclsh was installed on this older system. The text concludes with the details of how to add Tcl into the C language, and also how to design your own extensions to the language.

As Eric stated early in his book, Tcl development will continue. Also, several new versions have appeared since the 8.0 version covered by this text. In an email to me, he recommends a text by Ousterhout written in May 2008 as being the best book on the market which explains the language.

Learn from Demo's

At present, we have the most current version of the wish and tclsh programs already installed on our systems. However, so as not to be burdened with getting a text to type in the examples, I will show you how to get some powerful demos which will easily illustrate some of the features of the Tcl/Tk system.

If you enter "tcl/tk downloads" in Google, one of the sites which will be presented will be for the active state version:

pic

They have a free community version, and another which offers full support for serious developers.

Now the free community version is available in many flavors, as this next page clearly shows.

pic

The version I selected to download and install was the 8.6.0.0b3 version. This will arrive on your system in the familiar tar.gz format. Your goal is to install the system, and for this, you will need to become root in order to be allowed to have the system create the directories, into which it will install the system. A simple script is used to install the system. This is all done with these steps:

  1. Download ActiveTcl8.6.0.0b3.292682-linux-ix86-threaded.tar.gz
  2. gunzip ActiveTcl8.6.0.0b3.............tar.gz
  3. tar -xvf ActiveTcl8.6...........tar
  4. cd ActiveTcl8
  5. ./install.sh

The system will be installed into the directory: /opt/ActiveTcl-8.6

Under this, the bin directory will contain the interpreters wish 8.6 and tclsh 8.6, and the demos directory will contain various Tcl scripts. These will fully show the use of the many widgets and features of the Tk system.

pic

The key to getting to this set of demos is to enter the following command into xterm or another terminal window:

wish8.6 /opt/ActiveTcl-8.6/demos/Tk8.6/widget.tcl

Now you can really save some time by now looking at your history file and noting the number of this command. To again launch this system, you will only enter ! followed by the number history has assigned to this command. On my PCLinuxOS 2007, the number assigned to this was 999, while on my more recently installed 2010 version, it was 108.

pic

I also installed the system to my PCLinuxOS 2007, which is on another computer in my network. As that used tk 8.4, I just copied the wish 8.6 files and tclsh8.6 files into the location of the 8.4 files. Also, note that you are running the version of wish8.6 and tclsh8.6 which was pre-installed, and not the Active State version.

We will conclude this discussion of the demos with an example of the Rube Goldberg demo. The text states his quote to the effect that man will find a complicated means of performing a simple task.

A great example of this complexity is shown every time TCM (Turner Classic Movies) starts a movie. You are currently treated to a marble flowing and looping before the movie ultimately begins. Now in the Rube Goldberg demo:

pic

The ball drops which lights the match, causing the weight to fall on the cage door, which releases the mouse.

I hope you download this version and install it, as you will really enjoy this one animation. Also, these demos will allow you to examine the code to learn how the widgets are set up. All these show just how easily the Tcl/Tk system makes creating GUI applications. To learn more and develop your own, you may wish to pursue the Tcl/Tk documentation, which will cover the details of the newest versions of the system.

Now you may wish to visit the web site of the author of the text mentioned. When I pulled this volume off my shelves, I thought that I had heard the name before. Now, by examining the covers of the books he has written, we learn that he has made many contributions to those learning Linux and open source software. I soon found that I had also read another of his works called "Teach Yourself Linux." This, too, was easy to read and covered the topic completely.

pic