Next: tkconnect, Previous: Lisp Functions Invoked from Graphics, Up: General [Contents]

### 1.7 Linked Variables

It is possible to link lisp variables to TK variables. In general when the TK variable is changed, by for instance clicking on a radiobutton, the linked lisp variable will be changed. Conversely changing the lisp variable will be noticed by the TK graphics side, if one does the assignment in lisp using `setk` instead of `setq`.

    (button '.hello :textvariable '*message* :text "hi there")
    (pack '.hello)

This causes linking of the global variable `*message*` in lisp to a corresponding variable in TK. Moreover the message that is in the button `.hello` will be whatever the value of this global variable is (so long as the TK side is notified of the change!).

Thus if one does

    (setk *message* "good bye")

then the button will change to have good bye as its text. The lisp macro `setk` expands into

    (prog1 (setf *message* "good bye") (notice-text-variables))

which does the assignment, and then goes thru the linked variables checking for those that have changed, and updating the TK side should there be any. Thus if you have a more complex program which might have done the assignment of your global variable, you may include the call to `notice-text-variables` at the end, to assure that the graphics side knows about the changes.

A variable which is linked using the keyword `:textvariable` is always a variable containing a string.

However it is possible to have other types of variables.

    (checkbutton '.checkbutton1 :text "A button" :variable '(boolean *joe*))
    (checkbutton '.checkbutton2 :text "A button" :variable '*joe*)
    (checkbutton '.checkbutton3 :text "Debugging" :variable '(t *debug*)
                  :onvalue 100 :offvalue -1)

The first two examples are the same in that the default variable type for a checkbutton is `boolean`. Notice that the specification of a variable type is by `(type variable)`. The types which are permissible are those which have coercion-fucntions, See Return Values. In the first example a variable `*joe*` will be linked, and its default initial value will be set to nil, since the default initial state of the check button is off, and the default off value is nil. Actually on the TK side, the corresponding boolean values are `"1"` and `"0"`, but the `boolean` type makes these become `t` and `nil`.

In the third example the variable *debug* may have any lisp value (here type is `t`). The initial value will be made to be `-1`, since the checkbutton is off. Clicking on `.checkbutton3` will result in the value of `*debug*` being changed to 100, and the light in the button will be toggled to on, See checkbutton. You may set the variable to be another value besides 100.

You may also call

    (link-text-variable '*joe* 'boolean)

to cause the linking of a variable named *joe*. This is done automatically whenever the variable is specified after one of the keys

    :variable   :textvariable.

Just as one must be cautious about using global variables in lisp, one must be cautious in making such linked variables. In particular note that the TK side, uses variables for various purposes. If you make a checkbutton with pathname `.a.b.c` then unless you specify a `:variable` option, the variable `c` will become associated to the TK value of the checkbutton. We do NOT link this variable by default, feeling that one might inadvertently alter global variables, and that they would not typically use the lisp convention of being of the form `*c*`. You must specify the `:variable` option, or call `link-variable`.

Next: tkconnect, Previous: Lisp Functions Invoked from Graphics, Up: General [Contents]