You are looking at historical revision 22077 of this page. It may differ significantly from its current revision.

PS/Tk

PS/Tk provides an interface to the Tk toolkit, and is an effective tool for creating graphical interfaces.

Examples

Several examples can be found in the svn repository for this egg.

Simple Dialog

(require-extension pstk)

(define (celsius->fahrenheit item)
  (let ((number (string->number item)))
    (if (number? number)
      (+ (* number 9/5) 32)
      0.0)))

(tk-start)
(tk/wm 'title tk "Celsius to Fahrenheit")

(let* ((celsius (tk 'create-widget 'entry))
       (label (tk 'create-widget 'label))
       (button (tk 'create-widget 'button
		   'text: 'Calculate
		   'command: (lambda () 
			       (label 'configure 
				      'text: (number->string (celsius->fahrenheit (celsius 'get))))))))
  ; layout widgets in a grid
  (tk/grid celsius 'column: 2 'row: 1 'sticky: 'we 'padx: 5 'pady: 5)
  (tk/grid label 'column: 2 'row: 2 'sticky: 'we 'padx: 5 'pady: 5)
  (tk/grid button 'column: 2 'row: 3 'sticky: 'we 'padx: 5 'pady: 5)
  (tk/grid (tk 'create-widget 'label 'text: "celsius") 
	   'column: 3 'row: 1 'sticky: 'w 'padx: 5 'pady: 5)
  (tk/grid (tk 'create-widget 'label 'text: "is") 
	   'column: 1 'row: 2 'sticky: 'e 'padx: 5 'pady: 5)
  (tk/grid (tk 'create-widget 'label 'text: "fahrenheit") 
	   'column: 3 'row: 2 'sticky: 'w 'padx: 5 'pady: 5)

  (tk-event-loop))

Using Tk

This section contains some simple use cases for parts of the Tk toolkit.

Simple Widgets

Button

A 'button' displays a label or image and can be clicked to invoke its command.

(tk 'create-widget 'button 
    'text: "Label" 
    'command: (lambda () "action"))
Label

A 'label' is intended to display a single line of text.

(tk 'create-widget 'label 
    'text: "Label")
Labelframe

The labelframe is a container widget, displaying a title and line around the widgets within it.

The following sequence packs a label within a label frame, which is then displayed in the top window.

(define labelframe (tk 'create-widget 'labelframe 'text: "Frame"))
(tk/pack (tk 'create-widget 'label 
             'text: "around label") 
         'in: labelframe)
(tk/pack labelframe)
Scale

The scale widget displays a slider which takes a value along a range. The user can drag the slider to a desired value, and the programmer can get or set the value.

(define scale (tk 'create-widget 'scale 
                              'label: "Scale widget"
                              'from: -100
                              'to: 100
                              'length: 300
                              'orient: 'horizontal
                              'showvalue: #t
                              'tickinterval: 20))
(tk/pack scale)

The value of the scale can be retrieved (as a string) using:

> (scale 'get)
"68"

The value of the scale can be set using:

> (scale 'set 39)
Spin box

The spinbox is a widget allowing the user to select from a range of numbers using up/down arrows.

(define spin (tk 'create-widget 'spinbox 'from: 10 'to: 50))
(tk/pack spin)

The value of the spinbox can be retrieved (as a string) using:

> (spin 'get)
"18"

The value of the spinbox can be set using:

> (spin 'set 30)
"30"

Decorations: Relief and Borders

An example illustrating the ranges of border/relief type.

(require-extension pstk)
(require-extension srfi-42)

(tk-start)
(tk/wm 'title tk "PS/Tk Example: Label relief")
(tk 'configure 'borderwidth: 10)

(do-ec (: relief '("raised" "sunken" "flat" "ridge" "groove" "solid"))
       (tk/pack 
         (tk 'create-widget 'label 'text: relief
             'relief: relief
             'bd: 2    ; width of the relief
             'padx: 10 ; inner padding, between label and its relief
             )
         'side: 'left 'padx: 4))

(tk-event-loop)

Tips on Using pstk

By default, the program tclsh8.5 is called, but an alternative program may be provided as an optional argument to (tk-start). This feature is useful if you want to include tclkit in your application, so users don't need to install tcl/tk separately. For example, to instead use tclkit-linux-x86 begin the program with (tk-start "tclkit-linux-x86").

Under windows, there is a problem with keyboard input. Currently, the fix is to show a dialog box which is dismissed by pressing 'Enter' (not clicking!) directly after starting tk.

(tk-start "wish85")
(tk/message-box 'title: "starting program" 'message: "Press ENTER" 'type: 'ok)
(tk-wm title tk "PROGRAM") 
etc

It is helpful to put an exception handler around your tk code to prevent orphaned shells, especially when developing your program. e.g.

(handle-exceptions
  exn
  (tk-end)  ; make sure tk is closed in event of any error
  ; begin program
  (tk-start)
 ; rest of  gui setup
 )

Authors

Kenneth A Dickey, Nils M Holm and Wolf-Dieter Busch created the initial versions of pstk. This port to Chicken, plus some additions, is by Peter Lane.

License

Public Domain.

Requirements

Requires an installation of tcltk.

Version History