Tcl/Tk -The Swiss knife of GUI and Scripting

Tcl/Tk is something you'll find virtually on every Unix/Linux machine. Not many Windows users may have actually used it. Tcl/Tk is a powerful scripting language capable of generating intuitive GUIs with amazingly few lines of code. It is time tested and works on all major platforms. Tcl/Tk is simple, portable and extendible. Tcl/Tk works as well on Windows as it does on Unix/Linux. Even today with the explosion in the number of programming languages and GUI toolkits, Tcl/Tk firmly holds its place. In this article we will try to find out why it remains the choice of thousands of programmers.

The latest release of Tcl/Tk is 8.4. However, many users still use v8.0 and v8.3 .You can download Tcl/Tk from its site http://www.tcl.tk/. All links to websites, tutorials and other softwares are given at the end of this article.

Tcl (Tool Command Language)

This is a workhorse scripting language. In fact Tk itself was added as an extension to it. Thus, to do anything with Tcl/Tk, you need to master Tcl. One of the biggest advantages that Tcl has is its unbelievable simplicity. Some of the features of this language include:

 

  • On-the-fly byte code compiler
  • Fully inbuilt C like Math, string, file and I/O functions
  • Support for functions and procedures
  • Glob style and egrep style pattern matching
  • Threading support
  • Virtual File system support
  • Certainly a very own graphical Toolkit
  • Support for extensions

For a complete new bee, learning Tcl should not be more than a matter of couple of days. If you have any experience with scripting, say Unix/Linux shells, then learning Tcl should be just a matter of hours.
The best introduction to Tcl/Tk is the book, "Tcl and the Tk Toolkit" by the creator John K. Ousterhout. A very good and free interactive tutorial is also available from the internet called TclTutor. Tcl contains about 60 built-in commands. There is a hand full of syntax rules. Tcl does not have strict grammar rules. For example, a variable name like “xd#0121* ” is okay in Tcl. Windows users will agree that Tcl scripting is, unlike any other scripting (Vbscript or Jscript) they have done before. Support for pattern matching is very interesting, something which windows users will definitely want and love to learn. Tk is not the only toolkit available. Users can develop their own custom extensions and a lot of them already exist! In this article I have discussed examples of using Tk with Tcl.

Tk

Building GUIs doesn't get easier. Tk is a graphical toolkit for use with Tcl. It is a wonderful thing. It creates some cool looking GUI with very less code. Tk consists of a wise collection of widgets like frame, label, text, bitmap, images, buttons (push, check and radio), message, listbox, scrollbar, scale, entries (input boxes), menu and menu button.

In the first example, only seven lines of code were required to make a simple interface for viewing text. Note that the file "new.txt" must be in the same directory as script file. A better idea would be to pass the name of file as command line option itself. You can run Tcl scripts simply by double clicking under Windows or by typing 'wish myfile.tcl' at command/shell prompt. Notice that just with seven lines we have a small window, a scrollbar and a text widget that displays text from a file. The 'pack' command delivers the desired arrangement of widgets to the packer or geometry manager.

In the second example we create a simple image viewer with just seven lines of code again. This example has photo (built-in image type), a label to display it and a simple push button. Tcl/Tk has a built-in support for GIF, PPM and PGM image formats. When the user clicks 'Ok' push button, the 'exit' command is executed and window closes.

The third example does more than just displaying things. It displays a message, allows the user to select the font size and color. It contains three types of widgets, namely the listbox, label and push buttons. Here, 'bind' command is used to associate the particular widget event with handler Tcl code (Example 1).

The most versatile widget is 'canvas'. It enables users to draw various shapes, arcs, curves, texts, images and even widgets. Also, it supports basic functions like scrolling and girding. A canvas widget can even generate an Encapsulated PostScript description of its content, for printing or inserting into some other document. And if you missed something, you can always make it all by yourself by using existing features or by means of extensions (Example 2).

Programming with Tcl/Tk

As already mentioned Tcl/Tk is extremely easy to learn. It is lightweight; the interpreter for v8.0 is just around 1792KB, which is a steal for what you can do with it. Tcl/Tk programs are merely scripts, which may typically occupy several kilobytes, yet give your application a full-fledged and sophisticated GUI framework. It is worth mentioning here that a few months ago I downloaded a tiny distribution of Linux called mulinux. I had three floppies, one containing a tiny OS, the second one containing a tiny Xwindow system and the third had Tcl/Tk interpreter with scores of application. The creator's idea of using Tcl/Tk had paid off. The same disk now also had scores of cool graphical utilities (Tkpaint, Games, editor...) since these scripts might not have taken more than a couple of hundreds of kilobytes to store.

You will want to use Tcl/Tk if,
1. You have less time and more work (as always).
2. You need a portable GUI framework for your application.
3. You're looking for a versatile cross platform scripting language.
4. You are interested in creating very lightweight and cross platform applications.
5. You are looking for an excellent "glue language" with GUI capabilities.

The wish (window shell) interpreter is a really nice tool .You can see the GUI building as you type Tcl commands. Like Tcl, learning to use Tk is also easy. Most of the widgets have common options like background color, foreground color, font, image, text, border width, orient, relief etc., which can be easily remembered. You should only be aware of widget specific options and there are not many. Tk is unbelievably simple when compared to other GUI libraries in C/C++. You may want to learn Tcl just to use Tk once you see it in action. Tk is not recommended for graphics intensive applications like games. But that is not for which it was made. It gives best results when used for general-purpose applications, where speed can be sacrificed for development time.

Tcl/Tk Extensions

Tcl/Tk come with library packages, which enable creation of extensions. Discussion on writing extensions to Tcl/Tk is beyond the scope of this article. Classically extensions were loaded using Pkg_mkIndex procedure, which is part of standard Tcl/Tk distribution. Newer versions of Tcl/Tk interpreter support what is known as "stub enabled extension" which enables developers to distribute their extension as single binary (*.dll or *.so) file. Users can now access these extensions by simply loading them using load xyz.dll or load xyz.so. Here it is worth discussing some popular Tcl/Tk extensions.

Img extension: Traditionally Tcl/Tk supports gif and pgm/ppm image files only. The Img package extends this ability by providing support for BMP, XBM, XPM, GIF (with transparency, but without LZW), PNG, JPEG, TIFF and postscript image formats.
Tktable: TkTable is a table/matrix widget extension to Tcl/Tk. It is a valuable addition especially for those working with databases. Tktable package supports many features like cell validation, row and column titles, row & column spanning, multi-line cells, Unicode support etc.

Tclodbc: For ODBC, the Tclodbc contains all Tcl commands necessary for ODBC. Also it has some cool samples to start with.

FastBase MySQL + Tcl: Yes, MySQL Tcl interface. This requires only two files, FBSQL.DLL and LIBMYSQL.DLL. It is extremely easy to use since there are only ten commands. If you use MySql be sure to try this out.

Tkxml: Another interesting extension which I have not tried out is the Tkxml. This provides event based XML parsing functions via generic Tcl interface.

There are certainly many more extensions. You need to be a very good C programmer for in order to write Tcl/Tk extensions (Example 3).

Example 3. Illustrates use of fonts, listbox and buttons (my.tcl)

# my.tcl (example 3)

set cursz 10
set fontname "-Adobe-Helvetica-Medium-R-Normal--$cursz-*-*"
set fontcol "Sea Green"

message .msg -width 10c -justify left -bd 1 -bg $fontcol -font $fontname -text "Tcl is a \
intepreted language .It is easy to use and has amazing parsing and string handling capabilities \
.Tk is a GUI tool-kit for use with Tcl .Together they can create some amazing \
GUI like this one with few lines of code ."

pack .msg -side left -side top

listbox .fontsz
.fontsz configure -bg pink -fg blue
pack .fontsz -side left

set ctr 0
while { $ctr < 40 } {
.fontsz insert end $ctr
incr ctr 5 }

# Each of this button will change background color of text
button .one -text "Sea Green" -bg "Sea Green" -command {.msg configure -bg "Sea Green" }
button .two -text "Light Blue" -bg "Light Blue" -command {.msg configure -bg "Light Blue" }
button .three -text Pink -bg Pink -command {.msg configure -bg pink }
button .four -text White -bg White -command {.msg configure -bg white }
button .five -text "Peach Puff" -bg "Peach Puff" -command {.msg configure -bg "Peach Puff" }

pack .one .two .three .four .five -side top -fill x

# this code is executed when user double clicks on font-size list box
bind .fontsz <Double-Button-1> {
set cursz [selection get]
set fontname "-Adobe-Helvetica-Medium-R-Normal--$cursz-*-*"
.msg configure -font $fontname
}

Tcl/Tk Plugin

If you think that Java was the only language in which you could write applets, you are wrong. Yes, Tcl/Tk has a browser plugin, which enables viewing Tcl applets, popularly known as Tclets. Tclets are much simpler than Java applets. They are basically nothing more than Tcl/Tk programs and no customization is required. The Tcl Plugin is said to be the only programmable plugin. It is currently available for all major web browsers. You will be amazed to see the amount of interactivity this plugin can add to your site. Give it a try and I bet you won’t be disappointed!

Tcl/Tk IDE

And for those who need a IDE for generating GUI, there are a few. The most widely used in my opinion is Visual Tcl. Visual Tcl is a cross-platform application development environment for Tcl/Tk. It is itself written using Tcl/Tk and generates pure Tcl code. It also exports Tclets, which run in Netscape/MSIE. If you plan to use Tcl/Tk for creating complex GUI applications you'll really want this one. The package has elaborate installation instruction for various platforms, a small tutorial too.

Final Thoughts

People have their own reason for not liking Tcl/Tk. Some find it slow, while some argue that it does not support complex structures like linked lists and multidimensional arrays. Some of this is true. However, Tcl/Tk does run fast enough to produce smooth GUI programs. It may not be fast enough for complex mathematical or graphical work.

But Tcl/Tk has its own strong points, ease of learning, support for custom extensions and capability to generate amazing looking GUIs with few lines of codes. It is a unique blend of a scripting language and a graphical toolkit. Tcl/Tk programming can be highly addictive, especially if you love GUI programming without putting in too much. And I hope this article might have helped you in discovering this amazing language.

Links

http://www.tcl.tk/

Tcl/Tk and related software can be downloaded from this site .A great site for Tcl/Tk users .

http://www.msen.com/~clif/TclTutor.html

Download site for TclTutor .

http://pages.infinit.net/cclients/files/image10.zip
http://pages.infinit.net/cclients/files/Image-1.2-src.tgz

Download site for Img package (Windows/Linux)

http://www.purl.org/net/hobbs/tcl/capp/
http://tktable.sourceforge.net/

Tktable can be downloaded from this site .

http://sourceforge.net/projects/tclodbc

TclODBC download page .

http://www.fastbase.co.nz/

Site of MySql+Tcl .

http://tclxml.sourceforge.net/tclxml.html

Site of TclXml .

http://www.tcl.tk/software/plugin/download.html

You can download Tcl/Tk Plugin for various platforms here .

http://vtcl.sourceforge.net/

Homesite of Visual Tcl .








}