STK : First level

The first level of STK uses the standard Scheme constructs. To work at this level, a user must know a little set of rewriting rules from the original Tk-Tcl library. With these rules, the Tk manual pages and a little knowledge of Scheme, he/she can easily build a STK program.

Creation of a new widget (button, label, canvas, ...) is done with special STK primitives procedures. For instance, creating a new button can be done with

    (button ".b")
Note that the name of the widget must be ``stringified'' due to the Scheme evaluation mechanism. The call of a widget creation primitive defines a new Scheme object. This object, which is considered as a new basic type by the Scheme interpreter, is automatically stored in a variable whose name is equal to the string passed to the creation function. So, the preceding button creation would define an object stored in the .b variable. This object is a special kind of procedure which is generally used, as in pure Tk, to customize its associated widget. For instance, the expression
    (.b 'configure 
        '-text "Hello, world" 
        '-border 3)
permits to set the text and background options of the .b button. As we can see on this example, parameters must be well quoted in regard of the Scheme evaluation rules. Since this notation is barely crude, the Common Lisp keyword mechanism has been introduced in the Scheme interpreter [#!CLtl2!#] 1. Consequently, the preceding expression could have been written as
    (.b 'configure 
        :text "Hello, world" 
        :border 3)
which in turn is both close from Common Lisp and pure Tk. Of course, as in Tk, parameters can be passed at widget creation time and our button creation and initialization could have been done in a single expression:
    (button .b 
            :text "Hello, world" 
            :border 3)

The Tk binding mechanism, which serves to create widget event handlers follow the same kind of rules. The body of a Tk handler must be written, of course, in Scheme. Following example shows such a script; in this example, the label indicates how many times mouse button 3 has been depressed. Button press counter increment is achieved with the simple script given in the bind call.

    (define count 0)
    (pack 'append "." 
          (label ".l" 
                  :textvariable 'count) 
                  "fill")
    (bind .l "<3>" 
             '(set! count (+ count 1)))

To illustrate STK first level of programming, Figure 1 shows the simple file browser described in [#!Ouster-Tcl!#] written in STK .

Figure: A simple file browser
\begin{figure*}{\small
\vskip2mm
\begin{quote}
\begin{verbatim}...