A Few Basics
You've got Tcl/Tk up and running, so let's see how it works! The "%" sign is the prompt, it shows you that the console is waiting for you to tell it something to do. When you want to order Tcl to perform some action, you give it a command. Commands take the form of: command argument1 argument2... This means you type in the command plus however many arguments are desired. Make sense? If not, just hang on it should soon. The first program in any language is "Hello World" and involves making the computer write just that to the screen. (Just a quick note, if for some reason you want to quit Tcl at any time, just type exit)
In the console window, type in:
puts "Hello World." (note that text in bold represents what you should be typing or what the console is showing. For those of you with color, blue represents what you type in)
If all went right then you're screen should look like:
% puts "Hello World."
Note that (at least using Windows95), the console automatically turns what you're typing in blue. That's it! If you were programming in a "real" language, this would have taken you quite a bit longer to write, with Tcl you've just written your first program, and it is only one line! You may be thinking, "I don't see any buttons or graphics yet" but just wait that's for the next section. This will be quick, I promise.
Ok, what you've just written is in pure Tcl. The reason it didn't show up in the WISH box is because it didn't use any of the Tk extension commands. For now, we're going to just run a few more Tcl commands just to get the hang of it. You've just used your first command, called puts. All that puts does is write ("put") something to the screen. You told the computer, "Put 'Hello World' to the screen" and it did.
So what next? Variables. A variable is the computer's way of remembering something. Computers are dumb, you have to tell them exactly what you want them to do, so if you say "add two numbers" it will do that but if you say "now add 3 to that" it will not have a clue what you are talking about unless you tell it to remember the first answer! Tcl makes variables easy, all you have to do is tell the computer to remember something, and what that memory should be called. Let's try it out.
set My_Variable "Fred"
You should see:
% set My_Variable "Fred"
So what just happened? You told the computer to remember the word "Fred." Now, anytime you want it to tell you what it's remembering you ask for the variable My_Variable. You've also picked up your second command! The syntax is set variable value, hence My_Variable was the variable and Fred the value to put in that variable. Note that Tcl is case sensitive, so if you ask the computer to tell you what's in my_variable, it won't understand. Time for something a little more useful: first grade math!
set first 1
set second 3
expr $first + $second
You should see:
% set first 1
% set second 3
% expr $first + $second
Yes folks, you too can teach the computer to add! Ok, a lot happened here. For starters, we told the computer to remember two numbers. Then we used a new command with the syntax: expr operand operator operand. In this case our operands were the variables first and second. What's with those $ signs? Well, try it without them! Tcl should say something rude like, "syntax error in expression "first + second"." Eh? The dollar sign tells the computer that what follows is a variable, and you want the value of that variable. You don't want to add two words, you want to add what the computer remembers when you say "first" and "second." You can do lots more with expr, try multiplying or dividing. What follows is me messing around a bit:
% set first 5 ;#this tells the computer to remember 5 instead of 1
% expr $first + $second ;#add what's stored in the variable first with that of second
% expr $first + $first / $second ;#add $first to $first and divide by $second
% expr ($first + $first) / $second ;#oops, that didn't work right. Added parenthesis..
We've learned many things here. Your first question is probably, "what the heck does ;# do??" In truth, there are two things there: the # sign indicates that the line after it is a comment line. So if you type # set x 47 the computer won't do a thing, it sees the pound sign and ignores everything. This is handy for documentation of programs. The semicolon represents the end of the line. It's like hitting <Enter> or <Return>. This is why set x 1 set y 2 will give you an error but set x 1; set y 2 will work fine. In theory, using semicolons you could write a program, now matter how long, on one line. Why would you want to do this? Got me, but it's possible...
The second thing we've learned from this is that expr uses normal mathematical properties to determine what happens first. Since multiplication and division occur before addition and subtraction, 5 + 5 / 3 does not equal (5 + 5)/3! Also if you do the math, you'll notice that we should be getting decimals instead of whole numbers (5 + 5/3= 6.666.. while (5+5)/3= 3.333..). Tcl automatically converts variables! This gets a bit technical, but it's important so bear with me. When you make a variable, Tcl automatically makes it into a "type." This tells the computer whether it's an integer (1, 278, -4, etc), real number (decimals, 3.52 and the like), character (c, 3, $...) or string ("Fred" and other characters or phrases). I don't honestly know what other types there are, but Tcl will automatically convert variables of different types when needed. If you add an integer to a real, it will convert the integer to a real number (so 1 + 2.5 means 1.0 + 2.5). This isn't a huge deal, except that since we gave Tcl two integers, it gave us an integer back by truncating the real number answer! This is something to remember and be wary of...
There are many, many more commands and important things like loops and nesting but before we get there, let's do some fun stuff! Enough of this text, we want WIDGETS!
Copyright 1998 by Chris Palmer
Mail all comments to: Ardenstone@Ardenstone.com
Visit the rest of my Senior Seminar
or my homepage.