Tcl/Tk are John Ousterhout's Tool Control Language and Tool Kit.

Tk provides convenient, high-level interface/library for building Graphical User Interfaces (GUIs) under the X windows system.

Tcl is a scripting language for driving arbitrary unix tools, including Tk. It is normally implemented as an interpreter called wish.

sml-tk is an SML library that allows Tk to be accessed from SML. It uses Tk concepts and conventions for describing GUI's, but the SML language.

Interface is via an inter-process pipe. SML process generates Tk commands (actually strings) and sends them down pipe to a standard wish process, and also gets data back.

Tcl is good for writing short programs, but doesn't scale very well. Sml-tk lets you use ML to write underlying applications and to describe GUI structure.

tex2html_wrap_inline91 sml-tk documentation is on-line.

tex2html_wrap_inline91 Tcl/Tk documentation is online as man pages in /pkgs/tcltk4/man.

tex2html_wrap_inline91 You'll need access to Ousterhout's book ``Tcl and the Tk Toolkit.''

Tk Concepts

A GUI is described hierarchically. At the top level are one or more windows through which the user communicates with the underlying application.

Each window contains one or more graphical objects called widgets.

Types of widgets include:

tex2html_wrap_inline91 Text messages, labels, form entry fields, editable text displays,...

tex2html_wrap_inline91 Buttons, including radio-style, check buttons, menu buttons, etc.

tex2html_wrap_inline91 Menus, including menu buttons, popup menus.

tex2html_wrap_inline91 Canvases: screen regions on which graphical objects can be placed.

tex2html_wrap_inline91 Frames: collections of sub-widgets.

Each window and widget has a unique identifer string by which it can be referenced.

Widget Parameters

Each type of widget has a wide variety of possible parameters, depending on its nature. In general, nearly every type of widget supports:

tex2html_wrap_inline91 Configuration options, allowing specification of geometric characteristics, text fonts, colors, etc. The text contents of text widgets and certain call-back functions are also specified here.

tex2html_wrap_inline91 Packing options, indicating how the widget is to be placed in relation to its siblings in the enclosing window or widget.

tex2html_wrap_inline91 Binding options, specifying call-back functions to be invoked when interesting events occur in the widget's screen area.

Unfortunately, not all options are supported by all kinds of widgets. Some options are ignored, others may cause runtime errors, when applied to inappropriate widgets.

Interaction Model

In sml-tk, the programmer builds a description of the initial state and appearance of the GUI by constructing a datatype value (details in a moment). This description can built using pure functional programming.

The GUI is then started up by making a function call, passing this descriptive value as an argument. This start-up call doesn't return immediately. Instead, control passes to an event loop inside Tk.

tex2html_wrap_inline91 By default, the event processing code just sits doing nothing.

tex2html_wrap_inline91 When the user does something like press a keyboard key or mouse button, or move the mouse onto a graphic object, the event loop controller invokes an appropriate call-back function as specified in the GUI description.


A call-back function is just an ordinary SML function that is invoked by Tk when a particular event occurs.

tex2html_wrap_inline91 It usually takes as its argument an event value that indicates the details of the event that triggered it, such as the coordinates of the mouse at the time when a button was pressed.

tex2html_wrap_inline91 The function does any application processing required by the event. It can also make Tk library calls to change the appearance of the GUI, e.g., by adding, moving or deleting graphic objects in a canvas or widgets in a window.

Control returns to the main SML loop when a call-back function executes an appropriate function call to delete the top-level window (or on an error).

Text and Graphics

The built-in Tk widgets offer fairly sophisticated features for text and graphics.

For example, text in text widgets can be edited using the mouse to move around and to mark regions. The wish process maintains current knowledge of the contents of each text field. When the user performs an appropriate termination action (such as hitting RETURN), the associated call-back function must query Tk for the current text contents.

Graphics is performed in canvas widgets. Canvases are collections of canvas items, which can include

tex2html_wrap_inline91 Rectangles, Ovals, Polylines, etc.

tex2html_wrap_inline91 Icons (bitmaps or other images)

tex2html_wrap_inline91 Nested widgets

Each canvas item can have its own associated set of call-back functions to be executed when interesting events occur to that item, e.g., the cursor moves on or off the screen region associated with the item, or a button or key is pressed when the cursor is in that screen region.

The wish process maintains an internal concept of the location of each canvas item, which can also be queried by the SML callback functions.

SML datatypes (tk_types.sml)


Types for callbacks


SML Functions (export.sml)


Hints: CTRL/C is handy.

Ignore ``Warning: missed Binding'' messages.

Trivial Example Program


Andrew P. Tolmach
Thu May 8 16:09:06 PDT 1997