Home > Articles

An Overview of Tcl and Tk

  • Print
  • + Share This
This chapter introduces Tcl and Tk with a series of scripts illustrating their main features.
This chapter is from the book

This chapter is from the book

Although you should be able to start writing simple scripts after reading this chapter, the explanations here are not complete. The purpose of this chapter is to show you the overall structure of Tcl and Tk and the kinds of things they can do, so that when individual features are discussed in detail you'll be able to see why they are useful. All of the information in this chapter is revisited in more detail in later chapters, and several important aspects, such as the Tcl C interfaces, are not discussed at all in this chapter.

1.1 Getting Started

To invoke Tcl scripts, you must run a Tcl application. If Tcl is installed on your system, there should exist a simple Tcl shell application called tclsh, which you can use to try out some of the examples in this chapter. If Tcl has not been installed on your system, refer to Appendix A for information on how to obtain and install it.

You can start the tclsh application by opening a terminal window on a Macintosh or Unix system, or a command prompt window on a Windows system, and then entering the command

    tclsh

This causes tclsh to start in interactive mode, reading Tcl commands from the keyboard and passing them to the Tcl interpreter for evaluation. For starters, enter the following command at the tclsh prompt:

    expr 2 + 2

tclsh prints the result (4) and then prompts you for another command.

This example illustrates several features of Tcl. Each command consists of one or more words separated by spaces or tabs (referred to as whitespace characters). In the example there are four words: expr, 2, +, and 2. The first word of each command is the name of the command to execute. The other words are arguments that are passed to the command for processing. expr is one of the core commands provided by the Tcl library, so it exists in every Tcl application. It concatenates its arguments into a single string and evaluates the string as an arithmetic expression.

Each Tcl command returns a result. If a command has no meaningful result, it returns an empty string. For the expr command the result is the value of the expression.

All values in Tcl have a string representation and may also have a more efficient internal representation. In this example, expr's result is a numerical value that would have a binary integer or floating-point internal representation. The internal representation allows faster and more efficient processing of information. If the value simply is assigned to a variable or if it is used by another command expecting a numerical value, this is done very efficiently as no string conversion is required. Tcl automatically generates a string representation of a value on an as-needed basis—for example, when the value is displayed on the console.

From now on, we will use notation such as the following to describe examples:

    expr 2 + 2
  ⇒ 4

The first line is the command you enter and the second line is the result returned by the command. The ⇒ symbol indicates that the line contains a return value; the ⇒ is not actually printed out by tclsh. We will omit return values in cases where they aren't important, such as sequences of commands where only the last command's result matters.

Commands are normally terminated by newlines (typically the Enter or Return key on your keyboard), so each line that you enter in tclsh normally becomes a separate command. Semicolons also act as command separators, in case you wish to enter multiple commands on a single line. It is also possible for a single command to span multiple lines; you'll see how to do this later.

The expr command supports an expression syntax similar to that of expressions in ANSI C, including the same precedence rules and most of the C operators. Here are a few examples that you could enter in tclsh:

    expr 2 * 10 - 1
  ⇒ 19
    expr 14.1*6
  ⇒ 84.6
    expr sin(.2)
  ⇒ 0.19866933079506122
    expr rand()
  ⇒ 0.62130973004797
    expr rand()
  ⇒ 0.35263291623100307
    expr (3 > 4) || (6 <= 7)
  ⇒ 1

The first example shows the multiplication operator and how it has a higher precedence than subtraction. The second shows that expressions can contain real values as well as integer values. The next examples show some of the built-in functions supported by expr, including the rand() function for generating random numbers between 0 and 1. The last example shows the use of the relational operators > and <= and the logical OR operator ||. As in C, Boolean results are represented numerically with 1 for true and 0 for false.

To leave tclsh, invoke the exit command:

    exit

This command terminates the application and returns you to your shell.

  • + Share This
  • 🔖 Save To Your Account