openVote : Script or program?

With Tcl it is clear: all Tcl scripts are (interpreted) programs, real software, written by real men, even if they wear skirts. Even if they're not scottish. But how about Tk? When is a Tcl/Tk source an intelligent script and when is it a dumb program?

In fact, it is rather easy. You should just see the spark of it. I'll show you the spark.

openVote : Tcl/Tk Script

What you see here is a small Tcl/Tk script, dumping a few buttons on screen:

#! /usr/bin/wish

proc sync {} {
   
   do something 
}

wm title . "Waiter control"

set Data0 0

set Ctrl1 0
set Ctrl2 1

frame .input
frame .out1
frame .ctrl

button .ctrl.sync -text  "Sync" -command { sync }
button .ctrl.quit -text  "Quit" -command { destroy . }

button .out1.but1 -text "Data 0" -command {set Data0 [expr !$Data0]; sync }
checkbutton .out1.chk1 -variable Data0

checkbutton .input.chk1 -text "IN 1" -variable Ctrl1
checkbutton .input.chk2 -text "IN 2" -variable Ctrl2

pack .out1.chk1 .out1.but1 -side left -padx 10 -pady 5

pack .input.chk1 .input.chk2 -side left -padx 10 -pady 5
pack .ctrl.sync .ctrl.quit -side left -padx 10 -pady 5

pack .out1
pack .input
pack .ctrl
   

openVote : Tcl/Tk program

And this is the same source but now as a real program. Notice the subtle difference?

      
#! /usr/bin/wish

proc sync {} {
   
   do something 
}

proc run {} {
   
   wm title . "Waiter control"

   set Data0 0

   set Ctrl1 0
   set Ctrl2 1

   frame .input
   frame .out1
   frame .ctrl

   button .ctrl.sync -text  "Sync" -command { sync }
   button .ctrl.quit -text  "Quit" -command { destroy . }

   button .out1.but1 -text "Data 0" -command {set Data0 [expr !$Data0]; sync }
   checkbutton .out1.chk1 -variable Data0

   checkbutton .input.chk1 -text "IN 1" -variable Ctrl1
   checkbutton .input.chk2 -text "IN 2" -variable Ctrl2

   pack .out1.chk1 .out1.but1 -side left -padx 10 -pady 5

   pack .input.chk1 .input.chk2 -side left -padx 10 -pady 5
   pack .ctrl.sync .ctrl.quit -side left -padx 10 -pady 5

   pack .out1
   pack .input
   pack .ctrl
}

run
   

openVote : leaving out the cruft

OK, I will strip off all the non-essential lines and put the sources side by side:

Script Program
#! /usr/bin/wish



wm title . "Waiter control"

pack .out1
pack .input
pack .ctrl
      
#! /usr/bin/wish

proc run {} {
   
   wm title . "Waiter control"

   pack .out1
   pack .input
   pack .ctrl
}

run
      

The script, on the left, is in a gridlock. It runs but you have no way to influence more than a few side effects. There is no control loop. That's because the full source is written as if it was to do just one thing.

The program on the right has all payloads stored in proc's. And below the last proc definition, the real program starts. In this case, the program is just one line in length: the command 'run'. But you could put any sequence of Tcl instructions here.

Took me quite some time to come up with this approach. I have two Tcl/Tk manuals, each 3 inches thick (one in english and one in german) but it's very hard to find such things in these books. A lot of attention is paid to irrelevant functions, the interface to C, geometry managers, the lot. But this kind of simple thing is not really explained.

But that's what this site is for!

Page created on 3 April 2008 and

Page equipped with FroogleBuster technology