Tcl/Tk Language Reference
Exercise
Appendix
Appendix A : Answers to Exercises
Answer to Question
#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
Appendix B : Common Tcl/Tk Mistakes made by programmers
Due to the differences of Tcl/Tk syntax, many programmers who are well versed in other languages will make many mistakes when first programming in Tcl/Tk. Listed below are the most common ones.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.
Appendix C : Tcl/Tk in Unix
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" "$@"
Appendix D : About the Author
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 moc.liamg@avynnib. 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.
Appendix E : Codes
Almost all the programs are available in a zipped format for download. It is a 12.1 KB file.
Appendix F : FeedBacks
How do you like this tutorial? Pen some comments below.
Appendix G : What's new in Version 2 of this tutorial?
New widgetstk_optionMenu
panedwindow
spinbox
New Pages
Geometry Managers
Pack
Grid
Contents Page
Tk Commands
Bind
Others
Sample Programs
Made some additions here and there.
Corrected a lot of stupid mistakes I made in the last version.

Comments
I just wanted to say "Thank You" for this Tcl/Tk tutorial. I've followed
it and I can see how much time has been dedicated to it. As said, big THANKS
to you.
A Spaniard from Andalucia
This is an excellent, very understandable, tutorial. Thank you for taking the time to help "newbies" like me.
I bought the "Practical Programming in Tcl/Tk" book by Welch & Jones and had trouble understanding many of the concepts. You provided the complete code examples that clarified their sometimes obscure documentation.
I am now having fun writing small utilities in Tcl/Tk.
Frank
California, USA
My deep thanks to you for writing and posting this tutorial! I also bought a book on Tcl/TK and have been having trouble getting started with it. Already many of the things you have explained here have opened my eyes. I look forward to reading the rest of your tutorial!
BTW, I found two things so far that I have questions about.
1) In the test you provided, I used the $Price variable to do all the calculations in the script without trouble. Such as this:
set Price [expr $Hamburger + $Coke]
set Price [expr $Price*3]
set Price [expr $Price + $Pizza]
set Price [expr $Price / 3]
The results worked without error, however I am curious what downside there may be to doing it this way, other than the obvious of not having those other variables preserve their data from the various stages.
2) I do not get any error when put a { brace in my comment without a / forward slash. I'm using Tcl/TK 8.4. Do you knwo if this is something that was changed later?
Thanks again!
BenN
Thanks for the encouragement. :-)
Well, it is a matter of personal preference - if you want to do it this way its fine. The advantage with my version is that it is a bit more verbose. A bit more easier to read - I think.
[expr {$total / 3}]
See the { } around the calculation - that will make the calculation a bit faster - it is recommended that you use it.
The brace must be closed - if it is an open brace - it will create an error. For eg..
;# comment {
This will create an error
;# comment { }
No error - the opened brace was closed in the comment itself
;# comment \{
No error - brace was escaped
I have one question, and I'm not sure where to find the answer. Does Tcl/TK allow for section commenting? For instance in C++ the type of comment I'm looking for would be like this;
/*
All this
section
will be
commented
*/
if {0} {
... code to comment ...
}
Just figured I'd share! :)
That's unnecessary. The shebang "#!/usr/bin/env wish" is equally portable.
Thanks for writing such a great tutorial on Tcl/Tk. I have a small question for you.
Is it possible to run external program like Matlab while executing Tcl/Tk script?
Thanks in advance
gopal
if { value == 5 } { ... }
is usually also a mistake... this statement will always evaluate to false (comparing the string "value" with 5). Similarly on the loop.
the correct one should be: if { $value == 5 } { ... }
I've always made that mistake too (forgetting his excellency dollar sign).
Thanks for the tutorial, it's really helpful :)
Thanks for such wonderful TCL/TK tutorials. This has help a lot to me for learning TCL/TK.
Present I am beginner for this.
I have some queries, in my tcl script 10 proc are present and every proc has different work to do.
I want to print all 10 proc work on same text widget which help me to trac which all commands are executing.
can I do this?
Thanks in advance for your support.
Gangadhar.
Your tutorial was really helpful!I am a beginner to TCL programming and could learn a lot of basic commands.
However,I'm facing a problem with using the "wish" command from within a file, though the same command works perfectly in the tclsh prompt.
The following commands in the TCL command prompt ( invoked with the tclsh command) work and a new frame is created.
---
% wish
% frame .mv_ex
wm title . "Title"
pack .mv_ex
--
When written in a file "trial.tcl", the wish command doesn't seem to work and returns an error.
Contents of the file "trial.tcl"
--
#!/usr/bin/wish -f
frame .mv_ex
wm title . "Animation in canvas"
pack .mv_ex
---
Error returned when "trial.tcl" is executed.
--
tclsh trial.tcl
invalid command name "frame"
while executing
"frame .mv_ex"
(file "trial.tcl" line 3)
---
Please help!! Thanks in advance.
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.