Version 12 of Show me an example

Updated 2002-08-05 21:23:28

hello, world

The canonical first program (according to Brian Kernighan and Dennis Ritchie, creators of the C language) simply prints the message "hello, world". In Tcl, this program is:

     puts "hello, world"

To run the program, you have to put it in a file. Use your favorite text editor to create a file named "hello.tcl" .

(CL thinks there ought to be a graceful mention here of interactive interpretation, but has yet to figure out how to express it. The point is that there are even ways to use Tcl without a source file which holds a program).

Then use the Tcl shell to interpret your program. Depending on your operating system, the paticular version installation, etc. your Tcl shell could be named 'tclsh', 'tclsh8.3', or some other numbered variant. For this example, we will assume 'tclsh'. From a command line, type:

     tclsh hello.tcl

And you should see the message:

     hello, world

[Someone should point to an additional page that describes for developers what to do to get to a command line and what to do if a command line isn't an option - like on MacOS versions before [6-9] ]

Now let us examine your first program. All Tcl programs are made up of commands. Each command may take some number of arguments. We are using the 'puts' command, which will print a string. In our case, the string "hello, world" is in quotes, so it is a single argument to puts.

If we want to factor out data, to make this program better maintainable, we might proceed to

 set greeting "hello"
 set addressee "world"
 puts "$greeting, $addressee"

We have now introduced two variables, greeting and addressee. A variable is created if you assign a value to it (the set commands). A variable's value is retrieved if you prefix its name with a dollar sign. The Tcl parser substitutes "hello" for "$greeting" and "world" for "$addressee" in the argument to puts in the third line - even inside the quoted string that otherwise contains a comma and a space. This flexibility in string handling distinguishes Tcl from most other languages.

Notice the end of the variable names is not delimited. This is o.k. as the Tcl parser after a dollar sign picks up all letters, digits, and underscores for the variable name, and finishes that on any other character (the comma in that case). If you want fancier variable names (like "->" below), brace them:

 set -> "world"
 puts "$greeting,${->}"

But then again, maybe better not...

If you had more of such greetings and addressees, this style would be fit for you. At the moment, it's an overkill ;-)


If you are a Microsoft Windows user, running the simplest scripts is a little more complicated. Windows distinguishes between "console" applications, which run in a terminal window environment, and "windows" applications, which have a snazzy graphical user interface (GUI). The default Tcl/Tk installation on Windows runs all .tcl files in the wish shell, as a graphical application. Just put the file on the desktop or find it in the explorer and click on it. But since our simple script doesn't create any windows, and it isn't clear where the words "hello, world" would go, this program won't appear to do too much. You can run this program by starting tclsh from the start menu, then typing the command "source c:/hello.tcl". Note that the traditional Windows backslash must be replaced with a forward slash.

Another possibility on Windows: add the line

   catch {console show}

to your script. This will open a console window which says "hello world", even if you just double-click the hello.tcl file. -- RS


hello, world with a GUI

One of the great things about Tcl is that you also get to use Tk. The "hello world" program for Tk is almost as simple. We just have to define a command button which will perform the printing action, then use one of the geometry managers to position it on the screen. The program looks like this:

     button .b -text "Push Me" -command {tk_messageBox -message  "hello, world"}
     pack .b

And you run it using the 'wish' shell. (Windows users can just double click on the file.)

The first line of your program creates a pushbutton, named '.b' with the label "Push Me". Instead of our favorite 'puts' command, we create a Tk messageBox (a modal dialog) with our message. The second line instructs the pack geometry manager to display your pushbutton.

The button command returns the widget name it received, .b in this case. So

 we can rewrite the above into a one-liner, where we call ''pack'' with the result of the ''button'' command:
 pack [button .b -text "Push Me" -command {tk_messageBox -message "hello, world"}]

hello, windows!

If you're feeling really ambitious, you can emulate the Microsoft Foundation Class' hello sample with less than two dozen lines of Tcl/Tk code. (Including comments and white space!)

     #  Create the main message window
     message .m -text "Hello Tcl!" -background white
     pack .m -expand true -fill both -ipadx 100 -ipady 40

     #  Create the main menu bar with a Help-About entry
     menu .menubar
     menu .menubar.help -tearoff 0
     .menubar add cascade -label "Help" -menu .menubar.help -underline 0
     .menubar.help add command -label "About Hello ..." \
          -accelerator "F1" -underline 0 \
          -command showAbout

     #  Define a procedure - an action for Help-About
     proc showAbout {} {
          tk_messageBox -message "Tcl/Tk\nHello Windows\nVersion 1.0" \
               -title "About Hello"
     }

     #  Configure the main window 
     wm title . "Hello Foundation Application"
     . configure -menu .menubar -width 200 -height 150
     bind . "<Key F1>" {showAbout}

Note that this industrial strength GUI sample, including a popup dialog and a menu accelerator, is implemented in something like 250 lines of Win32 C++ code and resource files. The advantage to Tcl/Tk commands is, of course, that it also runs on UNIX (and maybe even on a Mac).

Yes, the Mac version works (but not F1, and the message box icon I see in the about box is weird - I'm using my own build of TclKit on Mac) --jcw


Running this script in wish on my RedHat 7.0 with tcl/tk8.3 says: "can't invoke "message" command: application has been destroyed". What am I doing wrong? --dma

That doesn't look like a tcl error message. To run this program enter the above text into a file hello.tcl and then invoke

     wish hello.tcl

you should then see the gui displayed.

Yes, it runs, including F1. -- DMA


Beginning Tcl - Arts and Crafts of Tcl-Tk programming