Navigation Logo 1.2  Interactive Execution Navigation Logo

 

 

Tcl, and its extension Tk, are interpreted. This means that to the programmer it appears as if the computer speaks Tcl/Tk. Programmers can enter commands and expect them to be carried out without any apparent need for translation. By interactive execution, I mean running Tcl/Tk scripts in just this way. The software that interacts with the programmer is called an interpreter. The same software runs prewritten Tcl/Tk scripts directly as described in the next section.

Tcl

The Tcl interpreter is often named tclsh?? where the question marks represent a version number. This version number may not be present.

Running under Windows 95, you can start the tclsh?? interpreter by just clicking on tclsh?? which can be found from the Start button. The effect is to bring up what looks like a DOS window with a % sign as the prompt.

Running under Unix, you will have to find out what the name for tclsh?? actually is and, possibly, what directory contains it. If you installed Tcl yourself, you will know this. Otherwise, ask your system administrator. Once you have the file name of the interpreter, you can execute it. The effect is the same as with Windows 95. The effect is to bring up a DOS window that is actually running the Tcl interpreter. The default prompt is again the % sign.

Here is a sample session as it would appear under either Windows 95 or Unix. Characters appearing after a % sign have been typed by a Tcl user. The rest has been printed by the Tcl interpreter:

% are you there?
invalid command name "are"
% puts "Hellow Orld"
Hellow Orld
% proc say_hello {} { puts "Hellow Orld" }
% say_hello
Hellow Orld
% proc say_hello {} { return "Hellow Orld" }
% say_hello
Hellow Orld

From the tclsh?? interpreter, you can execute any command that Tcl knows simply by typing it as shown above. Everything you type after the % prompt is a command line. After a command line has been completed with the return/enter key, that command line is executed. The interpreter prints whatever the puts command sends to standard output. It also prints error messages and anything returned from a procedure. When done processing a command line, it prints a new % prompt.

The window in which the above activity takes place can be called the command window. When you invoke tclsh?? from X Windows, it is the same window in which you entered the tclsh?? command. When you invoke tclsh?? from Windows, it is a new DOS window. ("DOS" here refers to the kind of window and is not a claim that DOS is controlling what you see in the window – Tcl is.)

The proc command shown in the example is used to define a new procedure named say_hello. After it has been defined, this procedure can be executed like any Tcl command. Because Tcl is interpreted, procedures can be defined and redefined at will.

In fact, in this example, the procedure, say_hello, is implemented and executed twice. The first execution generates an empty return string which is not shown by the interpreter. What is shown is the output from a puts command. The second execution returns the same string. Because the return string is nonempty, the interpreter shows it.

While using the tclsh?? interpreter interactively, it is possible to execute Tcl statements that have been entered into a file. Tcl's source command serves this purpose. This command takes one argument, a file name. The effect of this command is to execute the statements in the named file. For example, suppose a file named hello contains these lines:

proc say_hello {} {
  puts "Hellow Orld"
}
If hello is available in the current directory, then the following session is possible with tclsh??.
% source hello
% say_hello 
Hellow Orld

You can enter a complete pathname instead of hello. This may not be necessary in Unix if you switch directories before starting the interpreter. In Windows 95, you will have to switch directories after starting the interpreter. Use Tcl's built-in cd command, for example,

% cd "C:\Program Files\Tcl Scripts"
The quotes are required because of the spaces in the pathname. When writing Windows pathnames for Tcl, you should replace every backslash "\" with a slash "/." Tcl will do the right thing when it passes the pathname through to Windows.

You can terminate the Tcl interpreter in several ways. One way is to use the exit command. When you follow this command with an integer on the command line, that integer will be the return code given to your operating system.

Remark

Default Unix installations of tclsh?? are set up so that when the interpreter runs interactively, it passes all unrecognizable commands to a Unix shell and then returns the results from that shell to the user. This means you can use Tcl as a kind of Unix shell. Until version 8.0 of Tcl, there was no similar mechanism in Windows installations.

Although useful when Tcl is used interactively, the mechanism is not useful with direct execution of Tcl scripts because it isn't portable.

Tk

The Tk interpreter is usually named wish??. It can be run interactively the same way tclsh?? is. Because Tk is an extension of Tcl, the points made about tclsh?? above apply.

With Tk, however, you see an extra window. This is called the root window and is for use by end users. The root window and similar windows created by your script are where the action will be.

Tk's command window is not useful in production scripts. Its sole value is that it lets you experiment with Tk interactively. It is true that Tk's command window could be used like Tcl's command window to communicate with the user through UNIX/DOS-like input and output, but the point of Tk is to get away from such an interface.

Figure 1.2a shows an example of an interactive use of Tk through its command window. The root window appears at the top of the figure and the command window at the bottom. The root window is named "wish." Since the appearance of a command window varies greatly between Unix and DOS, only the text from that window is shown. This text shows that two commands were executed. The first created a label with the text "Hellow Orld" and the second posted that label to the root window.

Figure 1.2a: Putting a Label In the Root Window
% label .example -text "Hellow Orld"
.example
% pack .example 
% 

A slightly more complicated example is shown in Figure 1.2b. The example shows two additional commands. The first of these creates a button and the second posts that button to the root window. The user can "push" the button by clicking on it with the mouse. The button is set up so that when the user does this the root window vanishes and the interpreter stops executing.

Figure 1.2b: Putting a Button In the Root Window
% label .example -text "Hellow Orld"
.example
% pack .example
% button .abort -text "Go Away" -command exit
.abort
% pack .abort
% 

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.