Previous
Tcl/Tk Language Reference 
Next
Exercise 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
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 . 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 widgets
tk_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.
Previous
Tcl/Tk Language Reference 
Next
Exercise 

Comments

Anonymous at 26 Feb, 2007 08:59
Hi Binny,

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
Reply to this.
rmaynard at 11 Apr, 2007 01:44
Wonderful tutorial(s). Much more helpful than the books I've gotten. Your writing style makes the subject enjoyable.
Reply to this.
Frank Turner at 17 May, 2007 11:57
Binny,
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
Reply to this.
Ben Nitschke at 08 Jul, 2007 07:42
Binny,

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
Reply to this.
Binny V A at 09 Jul, 2007 01:11
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!

Thanks for the encouragement. :-)

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.

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.

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?

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
Reply to this.
Ben Nitschke at 14 Jul, 2007 12:39
Thanks Binny!

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
*/
Reply to this.
Binny V A at 14 Jul, 2007 01:42
No it does not. Sorry.
Reply to this.
Ben Nitschke at 15 Jul, 2007 11:30
I found a way to comment sections of code using the following command:

if {0} {
... code to comment ...
}

Just figured I'd share! :)
Reply to this.
Anonymous at 13 Aug, 2007 04:51
> exec wish "$0" "$@"

That's unnecessary. The shebang "#!/usr/bin/env wish" is equally portable.
Reply to this.
Liqian at 30 Oct, 2007 05:37
It is a great tutorial. I went through it and learned a lot. Thank you, Binny.
Reply to this.
Gopal Adhikari at 09 Dec, 2007 09:43
Hi Binny

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
Reply to this.
Binny V A at 09 Dec, 2007 11:44
Sure - just use this to open the external application...

if { [catch { set result [exec "metlab"] } err] } {
puts "Error $err"
}
Reply to this.
Edwin at 17 Apr, 2008 09:06
On Appendix B,

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 :)
Reply to this.
Binny V A at 19 Apr, 2008 11:16
Sorry about that - fixed now. And thanks for pointing it out.
Reply to this.
Gangadhar at 29 Apr, 2008 08:46
Hi,
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.
Reply to this.
Flute at 30 Apr, 2008 04:51
Hi!
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.
Reply to this.
Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed