On this page:
create-window
window?
show-window
hide-window
make-button
make-message
draw-message
make-text
text-contents
make-choice
choice-index
Version: 4.1

1.12 Simple Graphical User Interfaces: "gui.ss"

The teachpack provides operations for creating and manipulating graphical user interfaces. We recommend using the world teachpack instead.

Window A Window is a data representation of a visible window on your computer screen.

GUI-ITEM A GUI-Item is a data representation of an active component of a window on your computer screen.

(create-window g)  Window

  g : (listof (listof GUI-ITEM))

Creates a window from the “matrix” of gui items g.

(window? x)  boolean?

  x : any/c

Is the given value a window?

(show-window w)  true

  w : Window

Shows w.

(hide-window w)  true

  w : window

Hides w.

(make-button label callback)  GUI-ITEM

  label : string>

  callback : (-> event%  boolean)

Creates a button with label and callback function. The latter receives an argument that it may safely ignore.

(make-message msg)  GUI-ITEM

  msg : string?

Creates a message item from msg.

(draw-message g m)  true

  g : GUI-ITEM

  m : string?

Displays m in message item g and erases the current message.

(make-text txt)  GUI-ITEM

  txt : string?

Creates an text editor (with label txt) that allows users to enter text.

(text-contents g)  string?

  g : GUI-ITEM

Determines the current contents of a text GUI-ITEM.

(make-choice choices)  GUI-ITEM

  choices : (listof string?)

Creates a choice menu from choices that permits users to choose from some alternatives.

(choice-index g)  natural-number/c

  g : GUI-ITEM

Determines the choice that is currently selected in a choice GUI-ITEM; the result is the 0-based index in the choice menu

Example 1:

  > (define w

      (create-window

        (list (list (make-button "QUIT" (lambda (e) (hide-window w)))))))

  ; A button appears on the screen.

  ; Click on the button and it will disappear.

  > (show-window w)

  ; The window disappears.

Example 2:

  ; text1 : GUI-ITEM

  (define text1

    (make-text "Please enter your name"))

  

  ; msg1 : GUI-ITEM

  (define msg1

    (make-message (string-append "Hello, World" (make-string 33 #\space))))

  

  ; Event -> true

  ; draws the current contents of text1 into msg1, prepended with "Hello, "

  (define (respond e)

    (draw-message msg1 (string-append "Hello, " (text-contents text1))))

  

  ; set up window with three "lines":

  ; a text field, a message, and two buttons

  ; fill in text and click OKAY

  (define w

   (create-window

    (list

     (list text1)

     (list msg1)

     (list (make-button "OKAY" respond)

           (make-button "QUIT" (lambda (e) (hide-window w)))))))