Previous References |
Tcl/Tk Tutorial Appendix |
Next |
#Script to find average price set hamburger 15 set coke 5 set pizza 30 set sum [expr {$hamburger + $coke}] set threetimes [expr $sum*3] set total [expr {$threetimes + $pizza}] set price [expr {$total / 3}] label .price_average -text "All three people should pay $price Rs." pack .price_average
Commenting
In C++, Java, Perl, etc., comments can be put at the end of the line using the same symbol as used at the beginning of the line. An example in C++...
//This is a comment
c++;//This is also a comment
But in Tcl/Tk if one uses '#' symbol for comment at the end of a line, it would create an error.
set a 10 #Give a the value 10
- This is wrong. The sentence must end before the commend is given. Lets see the correct commenting style.
set a 10 ;#Give a the value 10
- This is the right style. Here, ';' ends the sentence so that the interpreter won't think that the comment is the part of the sentence.
Braces or Curly Brackets
I have lost count of how many times I have written this sentence in Tcl...
if ( value == 5 ) { .... }
The brackets used in Tcl is '{' and '}' - not '(' and ')' as in all other major languages. So watch out for that. The correct code is...
if { value == 5 } { .... }
Loop starting
Another problem is at loop starting. The following expression is correct in C,C++,Perl,Java and countless other languages - but not in Tcl/Tk.
if (i == 0)
{
...
}
In Tcl/Tk, the beginning bracket of the loop should come right after the test expression bracket - like this...
if {i == 0} {
...
}
Braces in comment
If a there is a brace in a comment, Tcl will report it as an error. For example
# This is a { brace.
will create an error. If you must have a brace in the comment, put it like this...
# This is a \{ brace.
If you want to use Tcl/Tk in Unix or Linux you made a good choice. Tcl/Tk was created for those OSes and is more compactable with them that it is with Windows or Mac. Although this tutorial is primarily aimed at windows users, I thought I could add a note on Unix here.
First make sure that you have BOTH tcl and tk. Verify this with the command "rpm -q tcl tk". Most distributions have Tcl - but some don't have tk. If you don't have tcl or tk, install these packages from your distribution CD or download from www.rpmfind.net.
Now make sure that the first line - #!/usr/local/bin/wish(for example) is correctly pointed to the wish executable. This can change in different systems. So the best approach would be to use an alternate method. Instead of pointing it directly at the executable, execute the 'wish' with the script file as the argument. This can be done with the following commands...
#!/bin/sh
#The next line executes wish - wherever it is \
exec wish "$0" "$@"
My name is Binny V A and I have been programming in Tcl/Tk for some time now. I was very impressed by this language that I have decided to write a tutorial on this subject - so I did. My very first tutorial. Any questions, suggestions, criticisms etc can be directed to . You can know more about me at my website. For all the programs that I made in Tcl/Tk, go to the Tcl/Tk page in my site.
Almost all the programs are available in a zipped format for download. It is a 12.1 KB file.
How do you like this tutorial? Pen some comments below.
Previous References |
Contents | Next |