The main Tcl/Tk program is wish. Wish stands for windowing shell, and with it you can create graphical applications that run on all these platforms. The name of the program is a little different on UNIX, Windows, and Macintosh systems. On UNIX it is just wish. On Windows you will find wish.exe, and on the Macintosh the application name is Wish. A version number may also be part of the name, such as wish4.1, wish42.exe, or Wish 8.0. The differences among versions are introduced on page xl, and described in more detail in Part VII of the book. This book will just use wish to refer to all of these possibilities.
Tk adds Tcl commands that are used to create graphical user interfaces, and it is described in Part III. You can run Tcl without Tk if you do not need a graphical interface, such as with the CGI script in Chapter 3. In this case the program is tclsh, tclsh.exe or Tclsh.
When you run wish it displays an empty window and prompts for a Tcl command with a % prompt. You can enter Tcl commands interactively and experiment with the examples in this book. On Windows and Macintosh, a console window is used to prompt for Tcl commands. On UNIX, your terminal window is used. As described later, you can also set up stand-alone Tcl/Tk scripts that are self contained applications.
The source Command
You can enter Tcl commands interactively at the % prompt. It is a good idea to try out the examples in this book as you read along. For longer examples you can find them on the CD-ROM and edit the scripts in your favorite editor. Save your examples to a file and then execute them with the Tcl source command:
source filenameThe source command reads Tcl commands from a file and evaluates them just as if you had typed them interactively.
Chapter 3 develops a sample application. To get started, just open an editor on a file named cgi1.tcl. Each time you update this file you can save it, reload it into Tcl with the source command, and test it again. Development goes quickly because you do not wait for things to compile!
UNIX Tcl Scripts
On UNIX you can create a stand-alone Tcl or Tcl/Tk script much like an sh or csh script. The trick is in the first line of the file that contains your script. If the first line of a file begins with #!pathname, then UNIX uses pathname as the interpreter for the rest of the script. The "Hello, World!" program from Chapter 1 is repeated in Example 2-1 with the special starting line:
#!/usr/local/bin/tclsh
puts stdout {Hello, World!}Similarly, the Tk hello world program from Chapter 18 is shown in Example 2-2:
#!/usr/local/bin/wish
button .hello -text Hello -command {puts "Hello, World!"}
pack .hello -padx 10 -pady 10The actual pathname for tclsh and wish may be different on your system. If you get the pathname for the interpreter wrong, you get a confusing "command not found" error. You can find out the complete pathname of the Tcl interpreter with the info nameofexecutable command. This is what I get on my system:
info nameofexecutable=> /proj/tcl/install/5.x-sparc/bin/tclsh8.0
Watch out for long pathnames.
On most UNIX systems this special first line is limited to 32-characters, including the #!. If the pathname is too long you may end up with /bin/sh trying to interpret your script, giving you syntax errors. You might try using a symbolic link from a short name to the true, long name of the interpreter. However, watch out for systems like Solaris in which the script interpreter cannot be a symbolic link. Fortunately Solaris doesn't impose a 32 character limit on the pathname, so you can just use a long pathname.
The next example shows a trick that works around the pathname length limitation in all cases. The trick comes from a posting to comp.lang.tcl by Kevin Kenny. It takes advantage of a difference between comments in Tcl and the Bourne shell. Tcl comments are described on page 15. In the example, the Bourne shell command that runs the Tcl interpreter is hidden in a comment as far as Tcl is concerned, but it is visible to /bin/sh:
#!/bin/sh
# The backslash makes the next line a comment in Tcl \
exec /some/very/long/path/to/wish "$0" ${1+"$@"}
# ... Tcl script goes here ...You do not even have to know the complete pathname of tclsh or wish to use this trick. You can just do this:
#!/bin/sh
# Run wish from the users PATH \exec wish -f "$0" ${1+"$@"}
The drawback of an incomplete pathname is that many sites have different versions of wish and tclsh that correspond to different versions of Tcl and Tk. In addition, some users may not have these programs in their PATH.
If you have Tk version 3.6 or earlier, its version of wish requires a -f argument to make it read the contents of a file. The -f switch is ignored in Tk 4.0 and higher versions. The -f, if required, is also counted in the 32-character limit on #! lines.
#!/usr/local/bin/wish -f
"c:\Program Files\TCL76\wish.exe" c:\your\script.tclThis starts c:\your\script.tcl as a stand-alone Tcl/Tk program.
source "Hard Disk:Tcl/Tk 4.1:Applications:MyScript.tcl"
source -rcrc resourceIf you don't want to edit resources, you can just use the Wish Source menu to select a script to run.
console hideDisplay the console like this:
console showThe console is implemented by a second Tcl interpreter. You can evaluate Tcl commands in that interpreter with:
console eval command
% myscript.tcl arg1 arg2 arg3In Windows, you can have a shortcut that runs wish on your script and also passes additional arguments:
"c:\Program Files\TCL76\wish.exe" c:\your\script.tcl arg1The Tcl shells pass the command-line arguments to the script as the value of the argv variable. The number of command-line arguments is given by the argc variable. The name of the program, or script, is not part of argv nor is it counted by argc. Instead, it is put into the argv0 variable. Table 2-2 lists all the predefined variables in the Tcl shells. argv is a list, so you can use the lindex command described in Chapter 5 to extract items from it:
set arg1 [lindex $argv 0]The following script prints its arguments (foreach is described on page 67):
# Tcl script to echo command line arguments
puts "Program: $argv0"
puts "Number of arguments: $argc"
set i 0
foreach arg $argv {
puts "Arg $i: $arg"
incr i
}
wish ?options? ?script? ?arg1 arg2?If no script is specified, then wish just enters an interactive command loop. Table 2-1 lists the options that wish supports: