Skip to content
Andrej Vodopivec edited this page Mar 20, 2015 · 2 revisions

Control variables are used to control many window properties. You can define string-var, float-var, boolean-var and int-var variables. The window variables are configured with :textvariable or :variable options.

:textvariable is used to control the text displayed in the window and :variable is used to control the state of the window.

In the following example we use the :textvariable to control the text displayed in a label and :variable to control the state of a checkbox. The on/off value of the :variable is set by the :onvalue and :offvalue option, which default to "1" and "0". A boolean-var could also be used as a :variable option for the checkbox.

The variables can be traced with (trace-var var fun). This will call the function fun whenever var is changed.

(defpackage :tk-user
  (:use :cl :tk)
  (:export :main))

(in-package :tk-user)

(defun main ()
  (with-tk-root (root)
    (setf (window-title root) "Variables")
    (setf (window-geometry root) "300x200+200+200")
    (let ((f (frame :parent root))
          (cb-var (string-var))
          (l-var (string-var)))
      (pack f :expand t :fill "both")

      (pack (checkbutton :parent f
                         :text "Checkbox"
                         :variable cb-var
                         :onvalue "enabled"
                         :offvalue "disabled"
                         :command (lambda ()
                                    (setf (var-value l-var)
                                          (format nil "Checkbox is ~a." (var-value cb-var)))))
            :padx 2 :pady 2)

      (pack (label :parent f
                   :textvariable l-var)
            :padx 2 :pady 2)

      (setf (var-value cb-var) "enabled")
      (setf (var-value l-var) "Checkbox is enabled."))))
Clone this wiki locally