Skip to content
Andrej Vodopivec edited this page Mar 13, 2015 · 6 revisions

Button

A button can contain text and/or images. The text displayed can be controlled with a :textvariable. Buttons have a default command, which can be configured with a :command option or with the bind-command function.

In the next example we have a button which counts the number of clicks.

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

(in-package :tk-user)

(defun main ()
  (with-tk-root (root)
    (setf (window-title root) "Buttons")
    (setf (window-geometry root) "200x100+100+100")
    
    (let* ((f (frame :parent root :relief "ridge"))
           (counter 0)
           (s-var (string-var))
           (b (button :parent f :textvariable s-var)))
           
      (pack f :expand t :fill "both")
      
      (pack b :padx 2 :pady 2 :expand t)
            
      (setf (var-value s-var) "Clicks: 0")
      (bind-command b (lambda ()
                        (setf (var-value s-var)
                              (format nil "Clicks: ~a" (incf counter))))))))

Function implemented for buttons:

  • (button-invoke b): triggers a button press event

Tcl/Tk documentation for button.

Checkbutton

A checkbutton has two states which can be controlled with a :variable. The state is changed when the button is clicked. In the following example we have a checkbutton and a label which traces the state of the checkbutton.

Checkbutton

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

(in-package :tk-user)

(defun main ()
  (with-tk-root (root)
    (setf (window-title root) "Checkbuttons")
    (setf (window-geometry root) "200x100+100+100")
    
    (let* ((f (frame :parent root))
           (c-var (boolean-var))
           (c (checkbutton :parent f :text "Option" :variable c-var))
           (l-var (string-var))
           (l (label :parent f :textvariable l-var)))
      
      (pack f :expand t :fill "both")
      (pack (list c l) :padx 2 :pady 2)
      
      (setf (var-value c-var) t)
      (setf (var-value l-var) "On")
      (bind-command c (lambda ()
                        (if (var-value c-var)
                            (setf (var-value l-var) "On")
                            (setf (var-value l-var) "Off")))))))

Tcl/Tk documentation for checkbutton.

Radiobutton

Radiobuttons usually come in groups. The group is specified with a shared :variable which is used to control the radiobuttons in the group. The variable will have the :value of the currently selected radiobutton as its value.

In the following example we have four radiobuttons in a group defined by the variable r-var. We use the trace-var function to specify a function which is called whenever r-var changes. The value of r-var is traced in a label.

Radiobuttons

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

(in-package :tk-user)

(defun main ()
  (with-tk-root (root)
    (setf (window-title root) "Radiobuttons")
    (setf (window-minsize root) '(200 200))
    
    (let* ((f (frame :parent root
                     :padding '(5 5 5 5)))
           (r-var (integer-var))
           (l-var (string-var))
           (l (label :parent f :textvariable l-var)))
      
      (pack f :expand t :fill "both")
      
      (dolist (rb '(("Small" 1)
                    ("Medium" 2)
                    ("Big" 3)
                    ("Bigger" 4)))
        (pack (radiobutton :parent f
                           :text (car rb)
                           :variable r-var
                           :value (cadr rb))
              :padx 2 :pady 2 :fill "x"))

      (setf (var-value r-var) 2)
      (setf (var-value l-var) "Value 2")
      
      (trace-var r-var
                 (lambda (ev)
                   (declare (ignore ev))
                   (setf (var-value l-var)
                         (format nil "Value ~d" (var-value r-var)))))

      (pack l :padx 5 :pady 5))))

Tcl/Tk documentation for radiobutton.

Clone this wiki locally