Tickle me: Tcl/Tk

So I was writing a set of 'Hello World' programs. as an introduction to my new 'Art of programming' topic. Doing so I ran into a version in Tcl/Tk. It was short enougfh to publish. Easy enough to make and test. And impressive enough to get enthusiatic about. This topic is about the wonderful language Tcl/Tk. Below are some samples, showing that GUI programming (which is a pain in the ass when done 'proper') is extremely easy and fast. In a matter of a few hours you can master the bascis of Tcl/Tk and construct graphical applications with such beauty that it is a waste of time and money to even buy a book on Qt or GTK.

Tcl/Tk is short for Tool command language + Toolkit. Tcl was made by John Ousterhout at Berkeley for his private use but it proved to be so versatile that many others adopted and extended it. After some exercises in the language you will easily understand why.

Modula-2 remains my favorite for compiled applications. But Tcl/Tk has won my heart for graphical stuff, despite it's resemblence to C. Luckily it looks just like plain C and not C++.

Some examples in Tcl/Tk

In the 'Programming' section you can find a Tcl/Tk version of 'Hello World'. Please visit that page in order to view it. The first example is a first version of the program to control the Parilux 1 device. It is fully graphical, user controllable and not yet finished. But it looks so fine and the source is so simple!

#!/usr/bin/wish

set Data0 0
set Data1 0
set Data2 0
set Data3 0
set Data4 0
set Data5 0
set Data6 0
set Data7 0

set Ctrl1 0
set Ctrl2 1
set Ctrl3 1
set Ctrl4 0

frame .frm1 -relief groove
frame .frm2 -relief groove
frame .frm3 -relief groove
frame .frm4 -relief groove
frame .frm5 -relief groove
frame .frm6 -relief groove
frame .frm7 -relief groove
frame .frm8 -relief groove
frame .frm9 -relief groove

frame .frm10
frame .frm11

button .out1  -text "Toggle" -command {set Data0 [expr !$Data0] }
button .out2  -text "Toggle" -command {set Data1 [expr !$Data1] }
button .out3  -text "Toggle" -command {set Data2 [expr !$Data2] }
button .out4  -text "Toggle" -command {set Data3 [expr !$Data3] }
button .out5  -text "Toggle" -command {set Data4 [expr !$Data4] }
button .out6  -text "Toggle" -command {set Data5 [expr !$Data5] }
button .out7  -text "Toggle" -command {set Data6 [expr !$Data6] }
button .out8  -text "Toggle" -command {set Data7 [expr !$Data7] }
button .quit -text  "Quit" -command { destroy .}

checkbutton .out1c -text "LED 1"  -variable Data0
checkbutton .out2c -text "LED 2"  -variable Data1
checkbutton .out3c -text "LED 3"  -variable Data2
checkbutton .out4c -text "LED 4"  -variable Data3
checkbutton .out5c -text "OC 1"   -variable Data4
checkbutton .out6c -text "OC 2"   -variable Data5
checkbutton .out7c -text "OC 3"   -variable Data6
checkbutton .out8c -text "Select" -variable Data7

checkbutton .in1 -text "IN 1"	-variable Ctrl1
checkbutton .in2 -text "IN 2"	-variable Ctrl2
checkbutton .in3 -text "IN 3"	-variable Ctrl3
checkbutton .in4 -text "IN 4"	-variable Ctrl4

pack .out1 .out1c -in .frm1 -side top -padx 2m -pady 2m
pack .out2 .out2c -in .frm2 -side top -padx 2m -pady 2m
pack .out3 .out3c -in .frm3 -side top -padx 2m -pady 2m
pack .out4 .out4c -in .frm4 -side top -padx 2m -pady 2m
pack .out5 .out5c -in .frm5 -side top -padx 2m -pady 2m
pack .out6 .out6c -in .frm6 -side top -padx 2m -pady 2m
pack .out7 .out7c -in .frm7 -side top -padx 2m -pady 2m
pack .out8 .out8c -in .frm8 -side top -padx 2m -pady 2m

pack .frm1 .frm2 .frm3 .frm4 -in .frm10 -side left -fill x
pack .frm5 .frm6 .frm7 .frm8 -in .frm11 -side left -fill x
pack .in1 .in2 .in3 .in4 -in .frm9 -side left -padx 2m -pady 2m 

pack .quit -side bottom -fill x 
pack .frm10 .frm11 -fill x
pack .frm9
   
With the '(check)button' word, you define a (check)button. With 'frame' you define a container that comprises a few buttons. With 'pack' you are a mason who puts the bricks (frames) into a wall (the window). The only tricky parts so far were the treatment of variables (EVERYTHING is a string!) and the packing of windows.

If you are a human being (like me) just make the file executable (chmod 755 partk2) and run it. This program is a feast for the eye. 65 lines of text and a full GUI program! I wish I found this earlier, instead of the very frustrating detour via Qt, GTK and what else. Even Murus is too complicated.

If you are not a human being (in most cases that is when you use The Other System (TOS) compared to Linux) you need to get yourself a Tcl/Tk package, install it, and make sure the wish and tclch executables are in your searchpath. But most people that browse this site are more or less Linuxy people.
The Gates Company is not in favor of Tcl/Tk. It is a free competitor of their VB stuff. And they dislike things that are free in any meaning of the word.

What it looks like

This is what it looks like. For the Linux users this isn't new. They can just run the above executable but if you are a TOSser things get more complicated. Still, Tcl/Tk is a cross platform programming language.

The GUI contains 8 frames that heach hold a Toggle button and a checkbutton. In principle the Checkbutton is an indicator only, but this is not strict. Click on the Toggle button and see what happens...

If you click on 'Quit' the program does just that.

This is a fantastic toolkit language. I ordered some books on the internet (there ar tutorials but these are very poorly made, even the one at http://www.tcl.tk, and this week the book Tcl und Tk (the german version of the original Ousterhout book). This book is clearly written by someone who knows how to write sourcecode and those of us that also have written sources just get drawn into the flow of the book. This book just sucks you in and pulls you through all the pages towards section 3.

More to come....


Page created on 4 August 2007 and

Page equipped with FroogleBuster technology