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 

Comments

Anonymous at 19 Sep, 2007 07:06
HI ,
How to use ssh from TK, i want to ssh to another machine when a button is clicked in TK widget.
expect doesn't work here with TK getting error for spawn itself.

Need help in this...
Reply to this.
Anonymous at 13 Oct, 2008 03:11
use expect
Reply to this.
Anonymous at 13 Oct, 2008 03:12
+ tcl)
Reply to this.
Z. Wagner at 05 Apr, 2009 01:45
With pure Tcl/Tk you cannot use interactive programs as ssh, you can send just a single command by ssh if you can authorize by key, not by password. You should rather use expectk (=Tcl+Tk+expect). Remember that expect's send is overwritten by Tk's send, you have to use exp_send instead.
Reply to this.
Anonymous at 30 Sep, 2008 08:37
Hi Binny,
Great work, and thanks a ton for the help, this stuff is really useful. Request a little clarification you have mentioned refering to a manual, in few of the previous pages you had links to the manual. i am unable to connect to the link. could you please send me an updated link if there is any or refer me to some book where i could get this information from.
Thank you.
Reply to this.
Anonymous at 17 Nov, 2008 08:51
Hello,
When I am trying to run the program on Linux, I am getting an error as below,
NTCADHOME: Undefined variable.

Could you please tell me what these error and variable are?
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




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