Basic Syntax of Tcl
Let us begin, as all other tutorials begin, with the "Hello World" program. Create a file called "Hello.tcl" and enter the following into it.
#!/usr/local/bin/wish #Make a label "Hello World" label .hello -text "Hello World" pack .hello
The first line - '#!/usr/local/bin/wish' is not needed in windows. In Linux, it tells the name of the script language processor. Don't understand what that means? Don't worry your gray cells over it. Just put it at the top of the file. But if you are planning to make a good portable script, don't use that line. Use the following one instead.
#!/bin/sh #The next line executes wish - wherever it is \ exec wish "$0" "$@"
Why? See Tcl/Tk in Unix for the reason.
The second line - This is a comment. Any line that starts with a '#' char is a comment. Comments are not of any use in the program. It is used by programmer to talk to themselves. A programmer cannot be expected to remember every thing a script does. So he uses a comment to write it down. Next time he edits the script, he can read the comment and understand what the program is for. It is good practice to make as much comments as possible.
The third line - 'label .hello -text "Hello World"' makes a label and writes "Hello world" in it. You can change the text to any thing you like. Note the structure of the command -
label - The name of the widget. A widget is a user interface object in X graphical user interfaces. Confused? Yes? Me too. Lets just say that it is the name of the object that appears on screen. There are many other widgets too. If you want to display a button, you use the button widget. For text, you use the text widget. For entry, you guessed it, the entry widget. If you want, you can see more about widgets.
.hello - The name assigned to the widget. Ever widget must have a UNIQUE name. This name will be used when ever that widget must be accessed. This is called the path.
-text "Hello World" - The option for this widget. This option says that this widget must be given the text "Hello World". Options change according to the widgets - a button widget will not have all the options of the label widget and vise versa. But there will be many common ones. You can keep writing other options can also be written here. For example, let us make a label for showing the text "Hell World". The other lines are same as the Hello World program.
label .hell -text "Hell World" -font courierfont -relief raised
In this example, a lot more options are used. The font option is used to tell which font must be used to make the text and the relief option tells whether the text should appear raised, sunken, flat etc. To know all the options for a particular widget, read the manual that comes with Tcl. It lists every widget and every option they have. If you are going to program in Tcl, you will find your self peeking into the manual every few minutes. The most important and most commonly used options are listed here.
So we have the final syntax.
<NameOfWidget> <path> ?<option 1> <option 2> ...?
The fourth line - pack .hello - this line tells the you how to pack the widget. This line asks the interpreter to pack the widget called ".hello". And the interpreter, who is more obedient than the average kid, does it. Now pack is a geometry manager. Another geometry manager is 'grid'. Personally, I like grid better.
Now Tcl puritans will raise a great hue and cry and say that this is not the way to print "Hello World". The "pure" method is the following...
#Print "Hello World"
puts "Hello World"
Putting things in perspective, I am teaching Tcl/Tk - not Tcl. The above is the tcl method of doing it. My method is the Tcl/Tk method of doing it. I know that the person who is reading this tutorial will cry out "Hold the phone, you mean Tcl and Tk are two different languages?!!"
Now Tk is not a language by itself - it is, how do you put it, an add-on, if you will, to Tcl. Tk was created to provide a graphical ToolKit for some applications. But Tcl is a complete language in itself. You won't be very wrong if you say that Tcl and Tcl/Tk is two different languages.
Now the above script is the method of printing "Hello World" in Tcl in UNIX. In Windows, you won't see a thing if you execute that with wish.blog comments powered by Disqus