Tcl/Tk Cookbook - Getting Started


Purpose
This section is a brief description of how to setup your environment to access Tcl/Tk and other extensions. It also introduces tclsh, the Tcl shell application and wish, the Tk shell window-based application. If you are new to Tcl/Tk programming, follow this with Chapters 1 and 2.

You will need

If you are an end user, then you will require to know if Tcl/Tk or any other extension that you want to use is installed for your system. If they are then you will need to know the absolute pathnames of the bin, include and lib directories for these. If not, contact your system adminstrator and refer them to the "Housekeeping".

Setting up Environment Variables

The binaries and libraries of Tcl/Tk and its extensions such as blt etc are generally installed under /usr/bin and /usr/lib. If this is not the case (i.e. they are compiled but not installed under /usr) , you need to set up a few environment variables in your .cshrc (for Unix C shell users - Bourne shell users should edit their .profiles and ensure that they export the set environment variables):

e.g., If the master directory is ~/tcl then

setenv TCL_LIBRARY 	~/tcl/itcl/lib/tcl7.4
setenv TK_LIBRARY 	~/tcl/itcl/lib/tk4.0
setenv ITCL_LIBRARY 	~/tcl/itcl/lib/itcl2.0
setenv ITK_LIBRARY 	~/tcl/itcl/lib/itk2.0
setenv IWIDGETS_LIBRARY ~/tcl/itcl/lib/iwidgets2.0
setenv EXPECT_LIBRARY 	~/tcl/expect/lib
setenv BLT_LIBRARY 	~/tcl/blt/lib

Refer to the README file of the extension you want to use for that package.

Your search path should be set up to pick up the correct binaries for tclsh, wish or itkwish etc. Make sure you include the pathnames in your set path in your .login or .cshrc or .profile file

Note: Programming syntax throughout this cookbook are for Unix machine.

Quick Tour

tclsh

The starting point to writing your own Tcl scripts is to familiarise yourself with Tcl syntax and learn the core Tcl commands. Take the first step by typing tclsh at command level in your commandtool/shelltool/xterm. Shell will invoke tclsh to prompt sign will change to % to indicate tclsh is ready to read your Tcl commands from the keyboard and pass them to the Tcl interpreter for evaluation.

Every Tcl command consists of one or more words, the first of which is the name of the C function to be invoked by the interpreter. The rest of the words in the command are passed as arguments for the C procedure. The C function provided by the Tcl library. Tcl library contains functions to provide a full set of programming features to Tcl such as variables, control flow etc. Use the Basics of Tcl application to explore these. You can write your own functions as well and register them with Tcl interpreter. Chapter 6 describes how. For now try the following simple commands to test and get a flavour of Tcl:

expr 10 * 5 
tclsh will print 50 and prompt you again.
expr is a core Tcl application for carrying out arithmetic operations. expr returns 1 for true and 0 for false for Boolean values when it evaluates relational operations. Full description of expr is presented in Chapter 1 Basics of Tcl.

Try:
% puts "Hi there" 

You will get:

Hi There
%

Each Tcl command is separated either by a newline or a semicolon. Backslash in a command tells Tcl that the command continues in the next line.

Try
% puts "Hi there going to \
next line " 

You will get:

Hi there going to  next line
%

Type

% exit 

to quit tclsh.

Note: Experiment with Basisc of Tcl application to learn all Tcl syntax and built-in commands that make Tcl a full programming language.

wish

Most applications would want to use Tcl as the basis for scripting and assembling their modules together but create their own commands based on one or more extensions of Tcl, in particular Tk, the toolkit of commands for building window-based Tcl applications. Without Tk, Tcl remains yet another scripting language.

Tk (and all Tcl) commands can be invoked within the tk windowing shell wish.

Try (at command level of commandtool/shelltool/xterm):

wish 

The result will look like: The wish mainwindow is placed within the xterm that invoked it. Note that the commandline prompt at the xterm has changed to the wish prompt.

Like other X Window System based toolkits, the graphical user interface building blocks of Tk consists of widget classes (e.g., buttons, scrollbars) and functions (methods) to create and manipulate them. A Tk application consists of hierarchy of widgets positioned within a single mainwindow, (picture above). The mainwindow is uniquely referred by a "." and all the other widgets have names that reflect their position within the hierachy.

At the wish prompt type 

button .b -text "Press ME" -command exit 

You will get:

.b
%
Type
%pack .b 
the result will be :


The first command "button" to wish invoked the function to create a button ".b" to be placed within the mainwindow ".". The command also specified that :

1. the button's label should read "Press Me" and

2. if the user interacts with this button (by pressing the left mouse button on it) then it should invoke the command "exit".

The second command "pack" invokes the geometry manager to compute the location and size of the child widget ".b" within the parent and causes the widget to appear on the display. Note that the mainwindow is resized to the size of the child.

You can further test this interactive mode by typing:

.b configure -background Red 

The result will be :

The above line invokes the method "configure" for the button ".b" to change its background colour to red. Pressing the button exits wish and returns the prompt back to xterm.

Note: Chapter 2, Basisc of Tk, introduces Tk widget attributes and their values and how to build a simple widget hierachy with many widgets and behaviours. The example is then split to introduce the unique and powerful Tk command send.

So far you were asked to issue Tcl and Tk commands interactively to tclsh and wish. However you would want to generate a script file containing a series of commands which could be passed to wish (note that wish can execute all tcl commands) for interpretation. To do this, invoke your favourite text editor (eg. vi), create a script file the first line of which should be #!/usr/local/bin/wish

Note: The above line assumes that wish is installed in /usr/local/bin. If it is located elsewhere in your system, you should give appropriate pathname for the system to locate <wish.. Refer to the description under "Cookbook Source" in Housekeeping.

Follow the above line with:

button .b -text "Press Me" -command exit
pack .b

Name this file button.tcl, make it an executable by typing

 
chmod u+x button.tcl 
in the directory containing the file. Then you can execute this code by typing button.tcl . When you do that the system invokes the wish shell and passes the file as a script for wish to interpret.

The result will look like:

Pressing on the "Press Me" button with the left mouse button exits the application, taking down the wish shell. Note that when you start a wish shell this way from a file, no wish command prompt appears on the statup window. So you cannot issue further Tcl/Tk commands interactively to the wish window as you did before. You can interact with the Tcl/Tk application as it is intended (in this case the simple pressing the button quits the application).