An Introduction to Tcl/Tk

Tcl is a software package which provides an extensible command-line interface and scripting language. Tcl is an acronym for Tool Command Language. Tk is a software package for building graphical user interfaces (GUIs). Tk is implemented as an extension to Tcl.

The following examples of using Tcl can be tried in any application using Tcl, or, equivalently, in wish or tclsh, which are applications which come with the Tcl/Tk distribution and which do nothing else but run the Tcl interpreter. The % sign is the tcl command prompt and bold-face is used to represent the typed text.

% # comment
% set a 32
32
% set b 3.1415
3.1415
% set c "hi there"
hi there
% expr $a + $b
35.1415
% set d  [expr $a*3]
96
% puts "d=$d"
d=96
% while {$d > 0} { set d  [expr $d-20]; puts "d=$d" }
d=76
d=56
d=36
d=16
d=-4
% set text {d=$d}
d=$d
% for { set n 5} {$n>0} {set n [expr $n-1]} {
>  puts "n=$n"
>}
n=5
n=4
n=3
n=2
n=1
%

Usually the script will be contained in a text file which is then read using the source command:

% source foo

Procedures can also be easily defined in Tcl

% proc plus {a b} {
     return [expr $a+$b]
  }
% plus 2 2.3
4.3