Applets are dead, long live Tcklets !


A computer without Java is like chocolate cake without mustard.

For months now, Sun has been touting Java as the last word on everything. Learn Java, Sun assures you, and you'll become the most sought after nerd in the known universe. Women will fall at your feet, wealthy business men will stampede to light your cigar and your acne will finally disappear. Learn Java, they croon, and when you say "Jump", Gates himself will hop all over the room like a frog in heat. And to top it all of, if the numerous gushy articles written about it are to be believed, learning Java is a cake walk. Even the corner grocery store owner, who till yesterday thought a 'hard drive' was four hours on the highway, can become an authority on it. Forget your grandma, Forrest Gump himself can set up a Java consultancy. God didn't rest on the seventh day, the marketing people assure you, instead He wrote Java. It proved so machine independent, they insist, that even the machines in Hell run it !!

Unfortunately, the truth is slightly different. I've learnt Java and no one's been tripping over my toes. Business men do stampede when they meet me, but maybe that's my deo at work and the security guards threw me out the last time I tried to barge in to meet Gates. And Java is definitely not learner friendly. They only way you can fully understand Java is if you already know C/C++ pretty well. The only person I've ever met who told me that he had learnt Java all by himself, without any knowledge of any other computer programming language also told me he regularly spoke with the ghosts of Martin Luther King and Napoleon !! Java is powerful, robust and machine independent, but user friendly ? No way !!

Maybe God did write Java, but He probably hasn't been able to learn how to use it !!

What is really needed now is a language which can be used over the Internet, is easy to use and very specific. After all, most Java applets I've seen on the 'net are Ticker tapes. Maybe it's because Java programmers have an unholy fascination with ticker tapes, or maybe it the only thing you can do with Java and still keep the code small ! Infact, what I've noticed is that small, user and learner friendly languages usually win over big bulky general purpose ones.

Let's take an example here, HTML.

HTML is probably the single most successful Web based language around. Just about anyone can learn how to code in it and just about everyone does. It may not be very powerful, it may not even have an IF statement, but the point is that simplicity and specialization sell. People like to use languages which are simple to learn, powerful to use and have been made with their area of expertise in mind. HTML is just a system of tags to put fancy fonts and images on the 'net, but it does what it's made to do and it does it well. Dbase is another case in point. With Dbase you can create working business applications real fast, but that's just about all you can do with it. You can't write a good wordproccessor with it and to my knowledge, no version of DOOM has ever been written using Dbase !

The world seems to be heading more and more towards specialization in computer languages. While powerful general purpose languages like C/C++ still reign supreme, other, more specialized languages are springing up like weeds. You now have languages written just for animation and sound, or scripting languages for a spread sheet or even just for one brand of server software. People seem to want languages that can be applied directly to the job at hand, with as little learning involved as possible. And that's where Tcl/Tk comes in...

But Tcl/Tk ?...

So far, there really haven't been a single successful language for the Web that's simple enough for the masses to learn and yet powerful enough to get some serious work done. As I've said before, Java is just too difficult to qualify as something that even your cat can use. Tcl/Tk does. ( But you'll never catch a cat writing code. Why ? 'cause cats aren't NERDS !! Now dogs on the other hand... )

Sun, In all it's wisdom, finally realized that Java doesn't really have that bright a future as far as newbie programmers are concerned. So they began scurrying around trying to find a substitute, something they could use as a stop gap measure while Java matures. So they created the Tcklet. What Sun has gone and done is write a Plugin for Tcl/Tk, a scripting language that's been around for years. Why recycle a language instead of creating a new one ? Because people hate having to climb the learning curve all over again. Writing the Plugin wasn't hard, after all, the code for the interpreter has been floating around for quite some time. Now that Tcl/Tk has a Plugin, you can put the scripts up on your page and you've got a Tcklet, just like that !!

Beware of programmers who carry screwdrivers...

The Perl v/s Tcl/Tk war has a fan follow only slightly smaller than the membership of the Bill Gates Bashing Club ( BGBC ). As always, we love languages that have a fanatical following, whether it's C, Perl or French. Battle always brings out the best in a language and it's users. What with people scrambling all over hurling baseless accusations and then having to prove them !! You learn a lot about a language that way.

Tcklets may prove to be the one major chink in Microsoft's armor. They do have an answer to Java in ActiveX but they've got nothing that even remotely matches the usability of Tcl/Tk. If Gates is smart, he should go out and hire either Jacob Levy, or John Ousterhout or license the Tcklets technology, because with Suns formidable marketing team behind it, Tcklets are most definitely the future.

Downloading the ingredients...

Go to Sun's site on the Internet and download the Netscape Plugin they have on offer. If you want to, you can also check out their list of Tcl/Tk examples. Some of them ( especially the one about the Eyes ! ) are really good !! The Tcl/Tk code is also downloadable.

You won't see a single word about Tcklets on the Netscape site. I think it's about time they got their heads out of the sand and saw the way the wind is blowing. Making the Tcl/Tk interpreter a part of the Netscape Navigator wouldn't be a bad idea. That's what we think anyway, and if they do, remember you heard it here first !

The Code...

Install the plugin before you run any of these examples, or you'll be very disappointed at the results of your labor !! Create a directory named zzz and cut 'n paste these example into it. Once you're through, run Netscape, enter the full address of the relevant file ( e.g. C:\zzz\b1.tcl ), press Enter and the output will be displayed on the screen. If you want, you can use the wish41 interpreter to run these examples by typing wish41 filename.tcl at the command prompt. The interpreter will have to be downloaded off the Web.

b1.tcl

button .a
pack .a

This is our very first tcklet and it's really very simple. When you run this program through Netscape, you'll see a button appear on screen and a rather good looking button at that ! ( Vijay Mukhi thought the button looked real good, the editor thought it looked real UGLY! They fought over the issue and finally V.Mukhi's version of the truth was put up. ( These lines have been inserted without his permission !!)) Go onto the button, click it, move in and out of it and you'll see that for the amount of code that went into it, the tcklet's output is quite impressive.

In the program, we started off with the line button .a, meaning create a button named .a. We've called the button .a instead of a plain and simple a because the syntax demands it. Now, if we were just to leave the program at that and not write anything further, then the button will not be displayed on the screen. To display the button we need the services of a 'packer', a program that will pack and place the button onto the screen. That's why we need the line pack .a. It's only now that we will see the button displayed. This means that you can define a hundred buttons if you want, but unless you pack them, they will not be displayed.

b2.tcl

button .a -text "gosh it's simple"
pack .a

If you want, you can add text to the button ( and make it look a lot better in the process ) by using the -text command. Type whatever you want written on the button in double inverted commas after the command and it will be displayed on the screen. Like text, there are a maddening array of options, some of which are shown below...

b3.tcl

button .a -text "gosh it's simple" -background red
pack .a

This button has the background set to the color red.

b4.tcl

button .a -text "gosh it's simple " -foreground blue
pack .a

This button has the foreground set to the color blue.

b5.tcl

button .a -text "gosh it's simple " -activebackground blue -activeforeground orange
pack .a

Here we've defined the activebackground and the activeforeground, so that when you move in and out of the button, your customized colors will appear.

b6.tcl

button .a -text "gosh it's simple " -foreground #ff00ff
pack .a

Instead of using a limited number of predefined color, we can use the # symbol ( which can also be used as a comment ) to define a new color. The format is #RRGGBB ( where the values of RGB are in hex in the range 0 to FF ).

b7.tcl

button .a -text "gosh it's simple " -bitmap hourglass
pack .a

In this example, we display a bitmapped image on the button and you'll notice that when we use a buitmap and some text, the text is not shown.

b8.tcl

button .a -text "hello" -underline 2
pack .a

We can also use the -underline command to underline a certain character. For instance, the above code will underline the first L in hello. That's because like in most programming languages, we count from 0, not 1, so H is 0, E is 1 and the letter underlined, L, is 2.

b9.tcl

button .a -text "first"
button .b -text "second"
button .c -text "third"
pack .a .b .c -side left

Here we've got three buttons, .a .b and .c. The pack statement packs all of the buttons at one go and because we've said -side left, they'll all be scrunched up against each other on the left side of the screen.

b10.tcl

button .a -text "This button is rather large"
place configure .a -x 50 -y 1 -width 8i

Instead of using pack to display our widgets ( the techy name for all these buttons, scroll bars and stuff ) on the screen, we use place and then use configure to change the attributes of .a. We then specify the x and y co-ordinates of the exact spot where we want the button placed along with it's width in inches ( 8I, you can also have c for centimeters and m for millimeters ). Pretty impressive for two lines of code, huh ?

b11.tcl

button .flat -text "flat" -relief flat      -borderwidth 5
button .groove -text "groove" -relief groove     -borderwidth 5
button .raised -text "raised" -relief raised     -borderwidth 5
button .ridge -text "ridge" -relief ridge     -borderwidth 5
button .sunken -text "sunken" -relief sunken     -borderwidth 5
pack .flat .groove .raised .ridge .sunken

This is the very last button example and here we display all the types of buttons there are. The button command -relief specifies their three dimensional geography.

b12.tcl

entry .a
pack .a 

Instead of a button, we have an entry box where you can enter data. Useful.

b13.tcl

scale .c
pack .c

In this exampe, we create a scale on the screen. We'll be using this scale a little later.

b14.tcl

scale .c -from 0 -to 255 -label "hell" -showvalue 1 -resolution .001
pack .c

This scale is a little bit more complicated. We've defined the range of values ( 0 - 255 ), we've given the scale a label or name. We've set -showvalue to 1 ( TRUE ) so that the values are displayed on the screen as we move along and finally we've specified the number of decimal places in the value.

b15.tcl

scale .c -length 200 -orient horizontal -tickinterval 20
pack .c

This final example display a scale 200 pixels large, where the tick interval is 20 and the scale is horizontal instead of vertical.

Now, we could have gone on for pages and pages about the millions of different options available, but instead, I think it's best we move on to more substantial things...

I just had an idea, why not follow in the tradition of Java and write a Ticker tape tcklet ? The following programs are lading up to the grand finale of a Ticker tape program.

b16.tcl

set z "hello"
button .a -textvariable  z
pack .a

Tcl/Tk is, unlike HTML, a complete programming language. So like all other programming languages, it has variables. In this program b16.tcl, we create our very first variable, z, by setting it to represent the string "hello" ( Remember, all strings in "" ). Notice that unlike other languages, we don't say z = "hello" or even Let z = "hello". Apparently, the creators thought this was a more intuitive way to program.

Instead of having the command -text after the button, we have instead -textvariable, where z is the text variable. When the interpreter ( or Plugin ) sees the command -textvariable and the variable z, it replaces z with the string it represents, i.e. hello. So once we've packed the button and displayed it, we get to see a button with hello written on it. Not terribly exciting, but it's just a start.

b17.tcl

button .b -text "Click" -command   { destroy .b }
pack .b

This program displays a button on screen with click written on it. When you click the button, the -command, command is called and the instructions in the braces are executed. Here the instructions are to destroy the button .b.

b18.tcl

button .b -text "Click" -command  zzz
pack .b
proc zzz { } {
 destroy .b 
}

Assume that you want a whole list of commands to be called when you click the button, then what's to be done? We could try and fit them all within the braces, but it would be better to create a procedure. So in this program, the procedure zzz is called whenever the button is clicked upon. The proc zzz accepts no parameters and in this case, executes only one line of code, destroy .b. But we could have had a hell of a lot more lines in there.

Okay, I've pulled the pin, now what ?..

b19.tcl

scale .s -variable r -label "Red" -command zzz -from 0 -to 255 -orient horizontal
scale .s1 -variable g  -label "Green" -command zzz -from 0 -to 255 
-orient horizontal
scale .s2 -variable b -label "Blue" -command zzz -from 0 -to 255 -orient horizontal
entry .t 
pack .s .s1 .s2 .t
proc zzz { ignored } {
global r g b
set c [ format "#%2.2x%2.2x%2.2x" $r $g $b]
.t configure -foreground $c
}

This is a program that displays three horizontal scales on the screen and a text entry box. As you move the labels marked Red, Blue and Green about, you change the color of the text that you've typed into your entry box. The code seems to be a bit complicated, but this is really quite easy. Let's get to it.

The first three lines define three scales, .s, .s1 and .s2. Each of these scales is represented by a variable, .s by r, .s1 by g and .s2 by b. Each time the scales are moves about, -command is activated and it calls the procedure zzz. At the same time, the variables, r, g and b hold the current positional value of the scales they represent.

Every time the procedure zzz is called, a new variable c is defined. The square brackets tell the interpreter to first calculate the values inside the brackets and put the final result in c. The format command formats the values of r, g and b to two digits and c is then set to these values. Finally, we configure .t's foreground color to the color calculated by us using the values held in r, g and b, the variables that represent the scales.

The ignored within the first pair of braces tells the interpreter that proc zzz doesn't accept any parameters and the statement global r g b informs the interpreter that the values of r, g and b can be used in zzz.

b20.tcl

button .b -text "Click" -command { .t insert end $n }
set n "Hello "
text .t -bg white
pack .b .t

This program displays a large text window on screen, with a button right on top. When you click on the button, Hello is displayed in the window. In the first line, we state that when the button is clicked upon, the value held in the variable n ( hello ) should be added to the end of the text in the text window .t. So every time you click, another hello is added to the ones on the screen.

The -bg in the third line is the same as -background.

b21.tcl

button .b -text "Click" -command { .t delete 1.0 }
set n "Hello  "
text .t -bg white
pack .b .t
.t insert end $n

In this program, instead of displaying hello every time we click, we delete one character from the left at every mouse click. The number after the delete command stands for < the line on which to delete>.. So 1.0 means delete character number 0 on line number one. And that's exactly what happens.

b22.tcl

button .b -text "Click" -command zzz
set n "Hello "
text .t -bg white
pack .b .t
proc zzz { } {
global n
.t insert end $n
.t insert end $n
}

Here we've put the code into a procedure and now each time we click, we're rewarded with two hellos !!

b23.tcl

proc zzz { } {
global n
.t insert end $n
after 500 zzz
}
set n "Hello "
text .t -bg white
pack .t
zzz

Running this program will fill your screen up with line upon line of cheerful hellos. Notice the command after. This command calls the procedure after 500 milliseconds ( The time specified by us ) from within the procedure itself ! This is what makes the procedure loop round and round. If, later on, when you're experimenting with the code and you see no output on the screen, but your computer becomes substantially slower, it means that the program is looping away, but not displaying any output.

b24.tcl

proc zzz { } {
global n
.t delete 1.0
.t insert end $n
after 500 zzz
}

set n "What a nice day! Get away from the computer ***** "
text .t -bg white
pack .t
zzz

This program is a lot like the previous one, the only differences being the presence of the delete command and the new value of n.

b25.tcl

proc zzz { } {
global n
.t delete 1.0
.t insert end $n
after 500 zzz
}

set n "What a nice day! Get away from the computer ***** "
text .t -bg white -wrap none
pack .t
zzz

This is it !! the final ticker tape program ! The only difference between this program and b24.tcl is the presence of the command -wrap none. This tells the interpreter not to allow the program to write anything on the next line. This little trick makes the program scroll, just like that !!

Some embellishments...

b26.tcl

set n  "What a nice day! Get away from the computer!"
set c #000000
proc zzz {ignored} {
global r g b
set c [format "#%2.2x%2.2x%2.2x" $r $g $b]
.t configure -foreground $c
}
proc bbb { } {
global n 
.t insert end $n 
.t delete 1.0 
after 500 bbb 
}                           
scale .c -variable r  -from 0 -to 255 -orient horizontal -command zzz
scale .d -variable g  -from 0 -to 255 -orient horizontal -command zzz
scale .e -variable b  -from 0 -to 255 -orient horizontal -command zzz
text .t -relief ridge -bg white -wrap none 
pack .c .d .e 
pack .t -side bottom
bbb

This program is simply an amalgam of b19.tcl and b25.tcl. You should be able to figure it out.

Embedding Tcl/Tk code into an HTML document is no great deal. Just type :-

<EMBED SRC=b26.tcl WIDTH=100 HEIGHT=400>

in your HTML file where b26.tcl is the name of the tcklet you want.

I haven't lost my mind, it's backed up on tape somewhere.

People may think I'm crazy to put so much faith in what some call the dirtiest language around, ( "Tcl/Tk is a write-only language" is one quip I remember ) But I have a feeling about this language. I think that Tcl/Tk will finally move programming out from nerdish back rooms and into the family living room. It's Tcl/Tk's inherent simplicity and power that will make it a winner. It may not be the best solution to our problems, but it's going to be the most widely used one. Count on it.

Another reason we like Tcl/Tk is because you can interface C with it. So what we've done is write programs that use Tcl/Tk and C to fetch HTML files from HTTP servers, send and receive mail etc. We'll continue with our experiments and put the finished code up on our site regularly.

Disclaimer : Jacob Levy wrote to us complimenting us on our tutorials thus far, so our account of the truth may not be wholly unbiased !!!

Oh, and by the way, if you like what we've said, do send mail to both Sun and Netscape asking them to whole heartedly support and promote Tcklets.


The above tutorial is a joint effort of
Mr. Vijay Mukhi
Ms. Sonal Kotecha
Mr. Arsalan Zaidi


Have any suggestions, comments, ideas, cracked code, feedback. Feel free to get in touch with us.

Move back to the Vijay Mukhi's Technology Cornucopia Page to learn more about the other new Internet Technologies.


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Bombay 400 034, India
E-mail:vmukhi@giasbm01.vsnl.net.in Tel : 91-22-496 4335 /6/7/8/9 Fax : 91-22-307 28 59
http://www.vijaymukhi.com