Home | Meeting
Robert Luoma / luoma@blu.org / February 15, 1995

Introduction to Tcl/Tk


Tcl (``Tool Control Language'') is a shell interpreter. It was made to be embedded inside a C program. The usual stand-alone interpreter is ``tclsh''

Tk basically adds the X11 interface to Tcl.. It provides a set of widets. It has a stand-alone interpreter called ``wish''

A major ftp site for all sorts of Tcl/Tk goodies is
ftp://ftp.aud.alcatel.com/tcl/ .

Tcl/Tk CD-ROMs are available from Info-Magic (1-800-800-6613) and Walnut Creek (1-800-786-9907).

Tcl and Tk were written mainly by John Ousterhout. He also wrote a book Tcl and the Tk Toolkit (Addison-Wesley, 1994, ISBN: 0-201-63337-X).

Another book, Practical Programming in Tcl and Tk , by Brent Welch is due in this spring (1995). A review draft is available for a while in the directory
ftp://parcftp.xerox.com/pub/sprite/welch/

D. Richard Hipp has written a toolkit for embedding Tcl/Tk scripts into a C program. It is available as et1_1.tar.gz in the ftp archive:
ftp://ftp.std.com/pub/drh/
This directory also contains a paper describing the toolkit (notes.ps.gz).

Most of my talk is stolen from the above three sources.


Tcl


Quick Example

#    example from D. Richard Hipp
# Change the name of every file in the working directory
# to all lower-case letters
foreach file [glob -nocomplain *] {
  set newname [string tolower $file]
  if {"$file"!="$newname"} {
    exec mv $file $newname
  }
}

Syntax

Tcl is a line oriented shell language. Commands are are seperated by a newline or ``;''.

Unless you are careful, Tcl will forget what happened on the previous line. You can continue a new line with a backslash ``\''. You can also nest a set of commands in curly braces ``{'' and ``}''.

If the first character of a command is ``#'', then it is a comment; remember that ``;'' can seperate commands.

Like UNIX shells, Tcl does variable substitutions using the dollar sign; i.e. for variable blah, the substitution occurs for $blah.

Square brackets do command substitution -- [command]

The backslash ``\'' is the basic escape character.

Most command and word delimiters are disabled within the regular "double-quote". You can quote literals inside curly braces ``{'' and ``}''. A ``}'' needs to be seperated by at least one whitespace from the next ``{''.


Some Tcl Commands

catch
trap errors
eval
concatinate arguments, then evaluate as command
expr
fork and execute UNIX program
foreach
loop construct over a list of a values
global
declare global variables
history
command line history control
if
conditional command (else and elseif
incr
increment a variable by an integer amount
lindex
fetch an element of a list
list
create a list out of the arguments
proc
define a Tcl procedure
set
assign (or get) a value to a variable
source
evaluate Tcl commands in a file
while
loop construct

There ``C''-like operators like ``+'', ``-'', ``&&'', ``||'' and functions like sin and pow. In script files, command-line arguments ( except for the file name) are stored in argv as one character string and the number of arguments is in argc; note that argv0 is the name of the script file. Tcl has ``regular expression'' matching.


Odds and Ends

There is a Tcl extension called expect for controlling interactive programs (without any modifications to the interactive program!). If you are curious, try playing around with scripts like kibitz, ftp-rfc, and weather. The program can be obtained from:
ftp://ftp.cme.nist.gov/pub/expect.tar.Z


Tk


Quick Example

#   everyone's favorite ;-)
button .b -text "Hello, World!" -command exit
pack .b

Tk Widgets

button        entry   listbox      message       scrollbar
canvas        frame   menu         radiobutton   text
checkbutton   label   menubutton   scale         toplevel

The general purpose graphics widget is canvas. It has a bunch of useful items like rectangles, circles, lines, polygons, Bézier curves, text. Even other widgets can be embedded in a canvas.

Similarly, the general purpose text widget is text. It have several hooks for indexing, marking and tagging text.

Generally the frame widget is useful for dividing up a window into (hopefully logical) section. The widget toplevel is similar to a frame, but the frame is new top-level window; a top-level window could even be put on another host's display.


Tk commands

After a widget is created, you need to tell the geometry manager to display; pack is the most common command for this purpose.

Tcl uses bind to bind X events to Tcl commands.

Key
key was pressed
KeyRelease
key was released
Button
mouse button was pressed
ButtonRelease
mouse button was released
Enter
pointer moved into a window
Leave
pointer moved out of window
Motion
pointer moved from one point to another within a single window

Various keyboard modifiers are Control, Shift, Lock, Meta, Alt. Mouse clicks can be modified by Double and Triple.


Embedded Tk


Quick Example -- hello.c

int main(int argc, char **argv) {
  Et_Init(&argc,argv);
  ET( button .b -text {Hello, World!} -command exit );
  ET( pack .b );
  Et_MainLoop();
  return 0;
}

ET Compile

First compile the et2c preprocessor and the et.o library files. The include paths may vary from system to system.

  cc -o et2c -I/usr/include/tcl et2c.c
  et2c -I/usr/lib/tcl -I/usr/lib/tk et.c >et_.c
  cc -o et.o -c et_.c
  rm et_.c
This step need be done only once. Next compile the program.
  et2c hello.c >hello_.c
  cc -o hello hello_.c et.o -ltcl -ltk -lX11 -lm
  rm hello_.c


ET commands

(from D. Richard Hipp)
Et_Init
Initializes the ET system
Et_ReadStdin
Cause Tcl/Tk to be read from stdin
Et_MainLoop
Execute the event loop
ET
Execute Tcl/Tk. Return ET_OK or ET_ERROR
ET_STR
Execute Tcl/Tk. Return an ephemeral string.
ET_DBL
Execute Tcl/Tk. Return a double.
ET_INT
Execute Tcl/Tk. Return an int.
ET_OK
A return code. Same as TCL_OK.
ET_ERROR
A return code. Same as TCL_ERROR.
ET_INCLUDE
Load external Tcl script into static string.
ET_PROC
Define C code for a new Tcl/Tk command.
ET_INSTALL_COMMANDS
Register new commands with the interpreter.
Et_Interp
The Tcl/Tk interpreter.
Et_MainWindow
The application's main window.
Et_Display
The "Display*" for the application.
cmd_name
Base name of the executable.
cmd_dir
Directory in which the executable is found.

Robert Luoma Thursday, 16 February 1995
Home | Meeting