Your First Tclet!

Your First Tclet!

Do you see that button labelled "hello", below? That's a Tclet. It is actually a very small Tcl program running inside the web browser you are using.

Try moving your mouse pointer over the button, and clicking on it. You should see the color change slightly when the mouse pointer enters and leaves the button, and the button appears to be pressed into the page when you click on it. These are visual cues to let you know that this button is actually alive and connected to a program.

What's really going on here? You are viewing a web page, written in HTML, the web's lingua franca. The HTML that makes up this web page contains a special embed statement that instructs the browser to load a Tcl program over the web and execute it inside a small area on the page. The embed statement used above looks like this:

<embed src=tcl/t1.tcl width=50 height=25>

You might also have noticed a delay until the button actually became visible. That's because your browser is starting the Tcl interpreter, loading the Tcl program over the web, and finally displaying the result -- this takes a little time. The Tcl program is stored in a file on the web site from which you loaded this page, and must be transported over the web. Your browser requests this file. When it arrives on your computer, the browser gives the file to the Tcl interpreter, which uses the contents as a program to execute. So: Tclets are programs that are stored in separate files on the web; they are loaded when you visit a page that contains an embed statement that refers to them, and are executed on your machine.

By the way, if you want to see the source of the Tclet (not the source of the HTML page) click on the orange ball symbol, above. It's a link that jumps to a page containing the source code.

This Tclet is very simple; it creates a single button with the label "hello", and displays it in the area designated by the browser. Let's examine what it looks like, just to get the flavor of it (don't worry if you don't understand what it actually does, I'll explain everything before too long):

button .b -text hello
pack .b

The first line creates a button named .b with the text hello. Every button or other visible part of a Tcl program (we call these visible parts widgets, by the way) has a name. All widgets have names starting with . (that's a single dot); widgets are arranged in a hierarchy starting with the . widget, which is created automatically for you when the program starts to run. The name .b indicates that this widget is a direct descendant of ..

The second line displays the button, using a geometry manager. Geometry managers are complicated animals, and we don't need to know much about them until later. Suffice it to say that the pack geometry manager is what actually makes the button appear on the display. Widgets don't appear on the screen until they are managed by a geometry manager, so if the program above didn't include the second line, you would not see the button on your display.

Another thing: we've seen that Tcl programs are written as a series of commands. Each command starts on a new line. Each command contains one or more words, separated by spaces. The first word is the name of the command to invoke; we've seen two so far, button, which creates new buttons, and pack, which invokes a geometry manager to display buttons and any other widgets. There are many other commands that I'll introduce you to as we have occassion to use them.

The other words in the command are arguments to the command; they affect how the command works and what it does. For example, the word .b is used as the name of the new button to create, and the two words -text hello say that the text of the label on the new button should be the string hello.