Previous
File Handling in Tcl/Tk 
Next
Button, Entry, Label 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
TK

TK

Now we move on to the graphical areas of Tcl - Tk. If you have ever tried your hand in programming graphical programs in C++, you are going to scream in anger when you see how easy it is to make graphical programs in Tk. You will be very angry that you had not found this language before. You are warned.

Now we go back to the Hello World program that I showed at the beginning.


#!/usr/local/bin/wish
#Make a label "Hello World"
label .hello -text "Hello World"
pack .hello

I am showing this part for the second time - I want to make sure that you know how this is done.

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.

.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.

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 'packs' it. Now pack is a geometry manager. Another geometry manager is 'grid'. Personally, I like grid better.

Now lets learn more about the individual widgets...

Previous
File Handling in Tcl/Tk 
Next
Button, Entry, Label 
Subscribe to Feed