Andreas Rozek Hints for Reading List of Recent Changes Guestbook Entry Contact the Author  Deutsche Version  HomePage Previous Topic Next Topic  First Page of Current Topic Next Page Previous Page

TkLua Clock

The TkLua program described on this page was among the first scripts used to test the proper (and continuous) function of the author's TkLua distribution for Win32 platforms (and the resulting processor load).


Screenshot of the TkLua example program "Clock"

Technical Description

The display consists of a single canvas widget (tkcanvas) with "fixed" graphical elements for the clock's face and periodically replaced graphical elements for the three handles.

Most of the work is done within function updateDisplay: two global variables per handle (e.g., HourHand and HourHand_Angle) store canvas index and actual angle of that handle. Upon every call of updateDisplay (i.e., once per second) the routine checks if any of the handles has moved since the last invocation - if so, the old canvas element for that handle will be replaced by a new one with the proper orientation (unfortunately, it is impossible to rotate existing canvas objects). Only the first invocation of updateDisplay does without this check (as there are no handle objects yet - these will now be created for the first time).

As the calculation of any clock handle's orientation is based on the actual time, setting the system time manually or automatically (e.g., because of "daylight saving") will be reflected on the display within a single second. However, because of the simplicity of the underlying timer function (which is used to invoke updateDisplay periodically), the display quite often seems to "skip" a second due to processing delays caused by the TkLua program itself or by other simultaneously running applications. Nevertheless, on moderately loaded systems the shown time should match the actual system time with a tolerance of approximately +/- 1 second.

The clock's tick marks and most of the handles (except that for seconds) are represented by closed rectangular polygons, whose nodes are brought into proper orientation with the aid of a bit of vector algebra. Only the second handle is represented by a single line, for the sake of simplicity.

Apart from the "main program", "Clock" contains the following global functions (in alphabetical order):

  • createTick (NodeList, Angle)
    takes a list with the coordinates of a line or a polygon, moves these nodes to the outer border of the clock's face, rotates them into the orientation given by Angle and creates a canvas line or polgon object (using the appropriate Tk command);

  • newHand (NodeList, Angle)
    takes a list with the coordinates of a line or a polygon, brings these nodes into the orientation given by Angle and creates a canvas line or polgon object (using the appropriate Tk command). The result returned by this function contains the "canvas id" associated with the new graphics object;

  • updateDisplay ()
    calculates the orientation of any clock handle from the actual time and updates the display accordingly;

The program itself is quite simple and should be self-explanatory enough to forego any further description.

Source Code

The source code of this program is available for download:

References

[1] Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
Reference Manual of the Programming Language Lua 4.0
(see http://www.lua.org/manual)
the reference manual contains any relevant information about the language itself, the set of standard libraries and its interface to the run-time environment;
[2] Waldemar Celes
tklua - Binding Tk to Lua
(see http://www.tecgraf.puc-rio.br/~celes/tklua/)
TkLua provides support for Tk widgets (allowing to construct graphical user interfaces) from within Lua scripts. The given web page describes the "basic concepts" behind TkLua and explains how to use it from within Lua scripts;
[3] Christian Vogler
TkLua (4.0a1)
(see http://www.cis.upenn.edu/~cvogler/lua/tklua.html)
since the original distribution of TkLua [2] has been developed for Lua versions prior to 4.0, Christian Vogler ported TkLua to the new Lua 4.0 API;
[4] Tcl/Tk 8.3.4 Manual
(see http://www.scriptics.com/man/tcl8.3/)
if you plan to use TkLua for your Lua script, you will hardly survive without the Tcl/Tk documentation - it can be viewed online at the given address (unless you already downloaded the Tcl/Tk distribution onto your system and installed it there);

Disclaimer

Please, consider also the author's Disclaimer!

http://www.Andreas-Rozek.de/Lua/Examples/Clock/index_en.html    (last Modification: 14.04.2002)