-->
Previous Table of Contents Next


Working with Tcl

To try out some Tcl programs, you should run the Tcl interpreter, called wish, which allows you to enter Tcl commands as if you were in a Tcl-based shell, which is what wish is.

The most interesting use of Tcl is to create graphical programs including widgets such as push buttons. To create a push button in Tcl, use the button command:


     button .b1 \

       -text "My first button" \

       -command { exit }


NOTE:  Because Tcl is a scripting language, you can use the backslash character, \, to extend a command on one line over many lines. This makes your programs easier to read.

The preceding command creates a button widget named .b1 (the leading period is important). Just like Linux uses the / character to mark the root directory, Tcl uses the period (.) to mark the root widget (your application’s main window). We’re then creating a button widget that sits like a subdirectory beneath the root widget.

The -command sets the Tcl command that will run when the button gets pushed. In our case, the exit command exits wish and our Tcl script.


NOTE:  It’s important to note that the code for the -command gets evaluated only when the button is pushed, usually sometime after the button is created and usually when the Tcl program is in another procedure. Because of this, local variables no longer have their values at execution time.

This can be very difficult to debug. We will show some workarounds in the sample code.


To get a widget to appear, we must pack it. The pack command takes a lot of parameters, including the name of the widget or widgets to pack:


     pack .b1


WARNING:  Tcl widgets don’t appear until you pack them.

Making Script Files for Tcl

You can put together a set of Tcl commands into a script file, just like C and Bourne shell scripts. The program to execute the script is again wish. The following script assumes that wish is located in /usr/bin (as it is for Linux).

To turn our first example into a working script, we do the following:


     #!/usr/bin/wish

     #

     # example1.tcl

     #

     # Create a button.

     button .b1 \

       -text "My first button" \

       -command { exit }

     pack .b1

     # example1.tcl

To show you more of a flavor of Tcl scripting, we put together the following file. In it, Tcl commands create a set of buttons that allow you to launch useful Linux programs like xman and xterm. The toolbar appears at the bottom of the screen and uses the override-redirect mode that prevents a window manager from placing a title bar around the window. Also, in honor of Windows 95, we place the current time at the end of the toolbar.

In our script, we create a number of procedures, called procs in Tcl. The exec_cmd procedure executes a text string as a UNIX command. We use the eval statement to deal with text-string issues and evaluate any Tcl variables within the command. Try this Tcl script without the eval in the exec_cmd procedure and you’ll see why we need it. (It has to do with evaluating the arguments as one string or as a command line; this is one area where Tcl is not intuitive.)

The update_time procedure gets the current time, using the UNIX date command, and then changes the text displayed in a widget (you pass update_time the widget name) and uses the after command to set up a callback, the update_time procedure, to get called after a particular amount of time. With Tcl 7.5, you can use the built-in clock command instead of calling the Linux program date.

The main part of the Tcl script creates a frame widget to hold all the buttons and then creates a set of buttons. The logo button quits the script when it’s pressed. We use the words Linux and your machine’s hostname for the text in the logo button.

The whole point of this Tcl script is to launch commonly used applications, particularly graphical ones. The buttons set up are listed in Table 10.6.

Table 10.6 Applications Launched from the toolbar.tcl Script

Button Launches
Manuals xman to view Linux online manuals
Mail elm (inside xterm window) to read mail
Shell xterm for a command-line shell
File Manager xfm, a Linux file manager
Images xv, an image-viewing and file-browsing program
Mahjongg Our favorite game on X


Previous Table of Contents Next