Previous: A simple editor application
Up: Example applications
Previous Page: A simple editor application
Next Page: References

A simple timer

This application is interesting, not because of its function as a timer, but rather because of its use of the TCL send command.

The way this time is implemented is to use two separate and independent TCL programs, called the timer and the ticker, which communicate via the use of send.

It is of course possible to implement a timer without the use of this feature,

The basic operation is as follows:

There are several things to note about thie pair of programs.

Firstly, the timing is not at all accurate. Most probably this is because of time slicing and the time taken to communicate between the two processes being significant. The timing accuracy should improve if the basic time units are increased in size, say to 5 or 10 seconds.

Secondly, the send command requires xauthority access to be set properly. This is a security feature, designed to minimize the risk that the feature will be misused, either accidentally or malevolently. If the programs do not work on your system, then check with the system manager to determine whether xauthority checking is turned on. Note - on large campuses, some machines may have the security feature turned on, and others not, which may prove puzzling at first.

Here is the code for the ticker:

# This script generates a ticker
        # which generates "Ticks" every second
        # Timing is VERY approximate as used here

label .lab1 -text "Ticker" -width 20 -relief raised entry .box -width 20 -relief sunken -textvariable data pack .lab1 .box -fill x

proc tick {} { # 1 second delay after 1000

# NOTE: it is NOT a good idea to use the code # form - # after 1000 {send timerP1.tcl Tick} # since after behaves differently in that # form. # See manual for details. }

set data [winfo name .] update tick set data TICKING

# Now set off repeated "Ticks" while 1 { tick send timerP1.tcl Tick }

and here is the code for the timer itself:

# This script generates a timer with start, stop 
        # and reset buttons.

# Set the 1 second ticker going... set x [exec ticker1.tcl &]

set stopped 1

label .counter -text 0.00 -relief raised -width 10 button .start -text Start -command { if $stopped { set stopped 0 } } button .restart -text Reset -command {reset} button .stop -text Stop -command {set stopped 1} button .quit -text "QUIT" -command {quit} pack .counter -side bottom -fill both pack .start -side left -fill both -expand yes pack .restart -side left -fill both -expand yes pack .stop -side left -fill both -expand yes pack .quit -side left -fill both -expand yes

set seconds 0 set hundredths 0 set stopped 1

puts [winfo name .]

proc reset {} { global seconds hundredths stopped set seconds 0 set hundredths 0 set stopped 1 .counter config -text [format "%d.%02d" $seconds $hundredths] update }

proc Tick {} { set x [exec echo "In Tick"] global seconds hundredths stopped if $stopped return set hundredths [expr $hundredths+100] if {$hundredths >= 100} { set hundredths 0 set seconds [expr $seconds+1] } .counter config -text [format "%d.%02d" $seconds $hundredths] }

proc quit {} { send ticker1.tcl {destroy .} destroy .}

bind . <Control-c> {destroy .} bind . <Control-q> {destroy .} focus .



Previous: A simple editor application
Up: Example applications
Previous Page: A simple editor application
Next Page: References

csstddm@brunel.ac.uk
Fri Aug 19 16:55:19 BST 1994