► |  | How to Design Programs Teachpacks

1 | Ht DP Teachpacks
2 | Ht DP/ 2e Teachpacks

▼ | 1 | Ht DP Teachpacks

1.1 | Manipulating Images: "image.rkt"
1.2 | Simulations and Animations: "world.rkt"
1.3 | Converting Temperatures: "convert.rkt"
1.4 | Guessing Numbers: "guess.rkt"
1.5 | Master Minding: "master.rkt"
1.6 | Playing Master Mind: "master-play.rkt"
1.7 | Simple Drawing: "draw.rkt"
1.8 | Hangman: "hangman.rkt"
1.9 | Playing Hangman: "hangman-play.rkt"
1.10 | Managing Control Arrows: "arrow.rkt"
1.11 | Manipulating Simple HTML Documents: "docs.rkt"
1.12 | Working with Files and Directories: "dir.rkt"
1.13 | Graphing Functions: "graphing.rkt"
1.14 | Simple Graphical User Interfaces: "gui.rkt"
1.15 | An Arrow GUI: "arrow-gui.rkt"
1.16 | Controlling an Elevator: "elevator.rkt"
1.17 | Lookup GUI: "lkup-gui.rkt"
1.18 | Guess GUI: "guess-gui.rkt"
1.19 | Queens: "show-queen.rkt"
1.20 | Matrix Functions: "matrix.rkt"

On this page:

create-window
window?
show-window
hide-window
make-button
make-message
draw-message
make-text
text-contents
make-choice
choice-index

top ← prev up next →

#### 1.14`` Simple Graphical User Interfaces: "gui.rkt"

(require htdp/gui)

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

top ← prev up next →