- tkprocedure
Arguments to tk are symbols or strings, except callbacks which are lists and last argument on the command line. (Except when a -validatecommand callback is being used with entry or spinbox widgets. In this case, the widget -command callback sequence can be placed before or after -validatecommand callback.) A callback is a quoted Scheme lambda that takes no arguments. '(lambda () ...) The body of the lambda can contain any expressions including (tk ...) commands.
(tk ...) returns all Tcl values as a string.
Be aware that callbacks do not have lexical scope. That is, the lambda's expressions can only access procedures and variables local to the lambda or at toplevel.
Widget command callbacks can have return values but do not return a value to Scheme with a few exceptions. Tcl ignores values returned from command callbacks (also with some exceptions).
Exceptions include entry/spinbox validation callbacks -validatecommand are required to return a boolean value (#t/#f, 0/1, indicating success or failure of validation). Some bind callbacks return values. Also widget command callbacks do return a value to the caller when invoked with (tk <widget path> 'invoke).
A tkgui program can create Tcl proc commands which can later be called from callback bodies or other code. Here's a simple example:
(import scheme.base nutils) (tk 'proc "x y" '(lambda () (let ((x (tk 'set 'x)) (y (tk 'set 'y))) (combinesp x y)))) ;; call the Tcl proc (define r (tk 'testing "hello," "out there") (writenl r) "hello, out there"
A proc command body can return numbers, strings or symbols. Values are returned as strings. Conversion to other types is straightforward.
tk can also create Tcl/Tk entities entirely in Tcl/Tk syntax:
(tk "proc test2 {x y} { puts \"x == $x, y == $y\" return [expr {$x + $y}] }") (writenl 'test2 (tk 'test2 43 54)) ;; -> x == 43, y == 54 ;; -> test2 "97"
Here's an example of using (tk ...) calls to show a frame and button:
(define topvar) ... (define frm (tk 'frame ".frm")) ;; frm -> ".frm" (define frm.btn (tk 'button (string-append frm ".btn") -text "Press me" '-command '(lambda () (set! topvar #t) ...)))
Procedures are provided to make tk programming more convenient: