next up previous contents index
Next: A more complex example Up: Tutorial Previous: Tutorial   Contents   Index

First steps

Let's start with the obligatory ``hello world'' type of program:

(defun hello-1()
  (with-ltk ()
   (let ((b (make-instance 'button 
                           :master nil
                           :text "Press Me"
                           :command (lambda ()
                                      (format t "Hello World!~&")))))
     (pack b))))

Figure 1: The window created by hello-1
Image hello-1

Let's go through it step-by-step. The whole code of hello-1 is wrapped into the with-ltk macro. It ensures that the GUI library is properly set up and the communication with the Tk toolkit is established. It runs the contained code which should initialize the GUI and after that calls mainloop to process the GUI events.

The next step is to create the button. This is done by creating an instance of the class button. The :text argument gives the text to display on the button and :command is the function to call, whenever the button is pressed. While the last two arguments should be obvious, the master needs explanation. In Tk, all GUI elements are arranged in a tree. So every GUI element has a parent node or ``master'' which designates its position in the tree structure. So put there the object that should be the parent for your button. Only for top level components nil may be given instead of an object.

For displaying any ltk object, a layout manager is used. There are two layout managers available, which can be used to arrange widgets in its parent, pack and grid. pack arranges widgets as a heap of boxes, which are horizontally or vertically stacked. grid arranges widgets in a table-like layout. NEVER use pack and grid for the same container, unpredictable behaviour may the result (or rather, the program will very predictably crash).


next up previous contents index
Next: A more complex example Up: Tutorial Previous: Tutorial   Contents   Index
Peter Herth 2006-01-29