Basics of the Tcl Language

You are not going to get a full rundown of the entire Tcl command set. Nor are you going to learn about every Tk widget and all it's options. Those things of course are left as an exercise for the reader. However we will learn some basic things about Tcl, and about Tk widgets. We will look at a few widgets in particular so we are familiar enough to construct something with them. If you would like to type along with these basic tutorials, you can just type 'tclsh' to start a Tcl interactive shell going and you will be presented with a '%' prompt.

A Tcl script consists of one or more commands. Commands are separated by newlines or semicolons. Each command is a set of words, the first word being the command name, and the following being the arguments.

Variables do not have to be declared, they are declared automatically when you try to set them. To declare a variable you type

set variable value

set a 123
When you use set, set also returns the value that was just set to the variable. The variables are referenced by $variable. So to use the variable type
puts $a

As you can see, the puts command is equivalent to C's printf.

To write a Tcl procedure you use the proc command.

proc procname {args} {procedure body}

proc plus {a b} {
	expr $a+$b
}
Once you have declared the procedure you just call it with the procedure name and whatever arguments are required, and it will return the value.

plus 1 2

Tcl has all the the control structures as any other language (if, while, for, and eval), it also has built in functions for dealing with expressions, lists, and arrays.

So far Tcl is boring...let's see some Tk