2017-05-12: GUI Programs with Sagittarius Scheme

Sagittarius Scheme is an implementation of Scheme capable of running R6RS and R7RS programs. In addition, it comes with a large library of its own. Sagittarius runs on several operating systems including Linux, Windows and Mac OS. In this post I look at how Sagittarius can be used along with the TCL/TK interpreter to write cross-platform GUI applications in Scheme.

Installing PSTK Library

In order to talk to the tcl/tk interpreter, Sagittarius must use the PSTK library. This library is available at r7rs-libs: the only file needed is "rebottled/pstk.sld".

For simplicity, libraries can be installed in the Sagittarius "lib" folder. This folder is located where you installed Sagittarius. In Linux this may be inside "/usr/local/share/"; on Windows, it will be where you installed Sagittarius, perhaps inside "Program Files".

Save the file "rebottled/pstk.sld" into the folder "lib/rebottled/pstk.sld".

PSTK Examples

There are several examples included with the library source code, in the folder "rebottled-examples/pstk/". As a simple test, download "hello.sps":

(import (scheme base)
        (scheme write)
        (rebottled pstk))

(let ((tk (tk-start)))
  (tk/pack (tk 'create-widget 
               'button 'text: "Hello"
               'command: (lambda () (display "Hello world") (newline)))
           'padx: 20 'pady: 20)
(tk-event-loop tk))

The pstk library assumes the tcl/tk program is called "tclsh". On Linux, this is probably already in your PATH. On Windows, you may need to install the program. A single-file version is also available, called tclkit; this is an old site, but the downloads work.

To use a program other than "tclsh", alter your Scheme script so it calls (tk-start PROGRAM)

You should be able to run the examples using a command on Linux like:

    > sagittarius hello.sps

or, on windows,

    > sash hello.sps

Note that some of the examples (such as the grid example and pendulum) use additional libraries found in the github link above, and you will have to add those to your "lib" folder (or use Sagittarius' -L option) to make the examples work.


With Sagittarius and a tcl/tk program in the PATH, Scheme programs can be run by double-clicking after associating the "sps" extension with Sagittarius. (Note that a terminal window also opens, which is an artifact of how Sagittarius has been built; always close your program from the GUI window, not by closing the terminal window, to avoid orphaning the tcl/tk shell. The terminal window can be useful for text output...)

A self-contained folder for your application can be made as follows:

  1. place any libraries you need in your Sagittarius "lib" folder
  2. save a program like "tclkit.exe" in your Sagittarius folder, alongside "sash.exe" (or require TCL/TK is installed on the target machine)
  3. save your Scheme program in the same location
  4. create a .bat file to launch your program, containing a single line like sash pendulum.sps

Once compressed the file comes to around 7MB, depending upon how many libraries you have in "lib". Decompress and double-click the .bat file to launch your application.

Some of the PSTK example programs running on Windows:

Page from Peter's Scrapbook, output from a VimWiki on 2024-01-29.