Go to the first, previous, next, last section, table of contents.

Guile Tk reference

Tk preliminaries

Some aspects of the Tk implementation for Guile are not yet in final form, so I will give you the recipe for using Tk in the guile-r0.3 release. Remember to check this section for changes in new releases of Guile.

This chapter is written assuming that you are running the guile program directly. If you are writing a C program instead, and linking with libguile, you can follow the instructions in section Libguile reference to initialize Tk correctly, and then follow this chapter when you write scheme code.

The following lines should be at the top of your scheme source file:

(require 'Gwish)

(use-library tcl)
(use-interface tcl)
(use-interface tclhack)

Your program will then typically create widgets and bind callbacks to the widgets. To then enter the Tk main loop, you need to call:

(tk-main-loop)

It is important to remember that Tk widget callbacks must always be specified via the special Guile primitive tcl-lambda, rather than the ordinary scheme lambda. This is for two reasons: first, procedures created by tcl-lambda know that they are still referenced as callbacks, and thus don't get garbage collected away. Second, Guile's Tk implementation is still based on Tcl, and tcl-lambda knows how to convert Tcl calling syntax to scheme.

This is expected to change as: (1) Sun works on making Tk independent of Tcl, and (2) the Free Software Foundation makes Guile independent of Tcl.

Creating widgets

The Tk package adds several primitive procedures to scheme; most of these correspond to Tk widget classes, and when invoked they create a new widget. Most of these procedures take the same basic set of options, but then each widget class has its own specific options.

Each widget type is described in detail in the very complete Tk man pages, so I will only give a brief description of them here.

The User Manual (see section `Creating a widget' in Guile User Manual) got you used to going back and forth between the Tcl/Tk and Guile/Tk calling conventions. The thing to remember here is that pathName arguments are always scheme quoted symbols, and the variables begin with a . (period).

Tk Widget Creation: button pathName [options ...]
Tk Widget Creation: canvas pathName [options ...]
Tk Widget Creation: checkbutton pathName [options ...]
Tk Widget Creation: entry pathName [options ...]
Tk Widget Creation: frame pathName [options ...]
Tk Widget Creation: label pathName [options ...]
Tk Widget Creation: listbox pathName [options ...]
Tk Widget Creation: menu pathName [options ...]
Tk Widget Creation: menubutton pathName [options ...]
Tk Widget Creation: message pathName [options ...]
Tk Widget Creation: listbox pathName [options ...]
Tk Widget Creation: radiobutton pathName [options ...]
Tk Widget Creation: scale pathName [options ...]
Tk Widget Creation: scrollbar pathName [options ...]
Tk Widget Creation: text pathName [options ...]
Tk Widget Creation: toplevel pathName [options ...]
Tk Widget Creation: update pathName [options ...]
These procedures create new widgets of the respective type. The options are described in the very detailed man pages for each widget, but also in the special options man page provided by Tk.

Tk Widget Creation: tk_popup menu x y [entries ...]
Posts a popup menu at location (x, y) on the screen. The menu widget should already have been created. The entry parameter is the index in the menu to which the mouse will point by default.

Widgets are destroyed with the destroy procedure:

Tk Widget Creation: destroy [windows ...]
Deletes the given windows plus all their descendants. If the root window "." is destroyed, then the entire application will be destroyed.

Widgets as procedures

Once you have created a widget, it automatically becomes a new scheme procedure. This procedure can be invoked with arguments, and such invocation will configure or modify the widget.

Tk Widget Configuration: WIDGET [options ...]
Configure WIDGET according to the options. Once again: options are described in detail in the options man page.

Geometry management procedures

The basic procedure used by Tk to arrange widgets on the screen is the packer. To talk to it, use:

Tk Geometry Management: pack option arg [args ...]
This geometry manager packas the child windows in order around the edges of the parent.

Tk is shipped with two other geometry management mechanisms: place, and a way of arranging objects in canvases.

Tk Geometry Management: place window option value [{option value} ...
Tk Geometry Management: place configure window option value [{option value} ...]
Tk Geometry Management: forget window
Tk Geometry Management: info window
Tk Geometry Management: slaves window
This geometry manager fixed placement of windows, where you specify the exact size and location of the master and slave. This is seldom used, since pack is the more common Tk geometry manager.

Tk widgets also interact properly with the window manager (as long as you have a good window manager). There is a procedure which allows you to explicitly talk to the window manager:

Tk Geometry Management: wm option window [args ...]
Interacts with the window manager, allowing the Tk programmer to set such resources as the title, geometry, resize increments, ... of that window. There are many possible values for option, documented in the Tk wm man page.

Event handling procedures

Whenever an action happens on the display that might be interesting to the Guile/Tk application (mouse motion, mouse clicks, keyboard events), Tk generates an event. The application can install event handlers to be invoked when these events occur.

Many of the event handlers are widget callbacks, which are treated in section Creating widgets; here we discuss other procedures that relate to event handling and flow control in Tk.

Tk Event Handling: bind widget "<BUTTON-ACTION>" callback
Binds the given button action to the procedure callback.

Tk Event Handling: fileevent input-port 'readable [callback]
Tk Event Handling: fileevent output-port 'writeable [callback]
Creates an event handler for the open file associated with input-port or output-port. The event handler invokes the given callback procedure when the file becomes available for reading (or writing, respectively).

Tk Event Handling: focus
Tk Event Handling: focus window
Tk Event Handling: focus option [args ...]
Manages the input focus. Without arguments it returns the path name of the focus window. With a single window argument it sets the focus to that window. See the focus man page for a description of the third form.

Tk Event Handling: grab [:global] window
Tk Event Handling: grab option [args ...]
Confine pointer and keyboard events to the subtree of the specified window. The second form has several behaviours according to the value of option, which can be current, release, set, status.

Tk Event Handling: tk-main-loop
Enters the Tk loop, which waits for events and invokes the appropriate callbacks. This is typically called after all widgets have been set up. It never returns until the . window is destroyed. This means that after invoking (tk-main-loop) in a Guile/Tk program, all future computation has to be done in widget callbacks.

Tk Event Handling: tkwait variable name
Tk Event Handling: tkwait visibility name
Tk Event Handling: tkwait window name
Wait for a variable to change, or a window to be destroyed.

Tk Event Handling: update ['idletasks]
Process pending events, flush all output, process when-idle handlers, until there are no pending events. If the 'idletasks symbol is passed, only process when-idle handlers.

Miscellaneous Tk procedures

Here are some procedures that come from the Tk module, and which do not belong in the above sections. As usual, complete details are available in the man pages.

Tk Misc: after ms
Tk Misc: after ms [commands ...]
Tk Misc: after cancel id
Tk Misc: after cancel [commands ...]
Tk Misc: after idle [commands ...]
Exececute the given commands after ms microseconds.

Tk Misc: bell [display]
Rings the bell on the display, or the default display if no display argument is given.

Tk Misc: bindtags window [tagList]
Returns a string describing the bindings associated with the given window.

Tk Misc: clipboard cmd [args ...]
Allows manipulation of the Tk clipboard. The cmd argument selects what type of action is taken on the clipboard. This can be clear or append.

Tk Misc: exit [returnCode]
Exits the process. If returnCode is not given it defaults to 0.

Tk Misc: image option [args ...]
The image command can create and manipulate images. The option parameter can be create, delete, height, names, type, width. The builtin image types are bitmap (for black and white images) and photo for full-color pixmaps.

Tk Misc: lower window [belowThis]
Without arguments, lower will place the given window under all its siblings in the stacking order. If belowThis is a window pathname, then window will be placed right underneath belowThis.

Tk Misc: option add pattern value [priority]
Tk Misc: option clear
Tk Misc: option get window name class
Tk Misc: option readfile fileName [priority]
Add (or retrieve) window options to (or from) the option database.

Tk Misc: raise window [aboveThis]
Without arguments, raise will place the given window over all its siblings in the stacking order. If aboveThis is a window pathname, then window will be placed right above aboveThis.

Tk Misc: selection option [args ...]
Allows manipulation of the X selection.

Tk Misc: send [options ...] app cmd [args ...]
Arranges for a command cmd to be executed in the application app.

Tk Misc: tk option [args ...]
Manipulates internal state variables in Tk.

Tk Misc: winfo option [args ...]
Retrieve information about Tk's windows. The option argument specifies what type of information; it can be 'atom, 'atomname, 'cells, 'children, 'class, 'colormapfull, 'containing, 'depth, 'exists, 'fpixels, 'geometry, 'height, 'id, 'interps, 'ismapped, 'manager, 'name, 'parent, 'pathname, 'pixels, 'pointerx, 'pointerxy, 'pointery and many others listed in the winfo man page.

Go to the first, previous, next, last section, table of contents.