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

1.14 Simple Graphical User Interfaces: "gui.rkt"🔗ℹ

 (require htdp/gui) package: htdp-lib

The teachpack provides functions for creating and manipulating graphical user interfaces. We recommend using 2htdp/universe 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.

procedure

(create-window g)  Window

  g : (listof (listof GUI-ITEM))
Creates a window from the “matrix” of gui items g.

procedure

(window? x)  boolean?

  x : any/c
Is the given value a window?

procedure

(show-window w)  true

  w : Window
Shows w.

procedure

(hide-window w)  true

  w : window
Hides w.

procedure

(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.

procedure

(make-message msg)  GUI-ITEM

  msg : string?
Creates a message item from msg.

procedure

(draw-message g m)  true

  g : GUI-ITEM
  m : string?
Displays m in message item g and erases the current message.

procedure

(make-text txt)  GUI-ITEM

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

procedure

(text-contents g)  string?

  g : GUI-ITEM
Determines the current contents of a text GUI-ITEM.

procedure

(make-choice choices)  GUI-ITEM

  choices : (listof string?)
Creates a choice menu from choices that permits users to choose from some alternatives.

procedure

(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)))))))