在本页中:
create-window
window?
show-window
hide-window
make-button
make-message
draw-message
make-text
text-contents
make-choice
choice-index

1.14 简单的图形用户界面: "gui.rkt"

 (require htdp/gui) package: htdp-lib

本教学包提供创建和操作图形用户界面的函数。我们建议改用2htdp/universe

Window Window是计算机屏幕上可见的窗口的数据表示。

GUI-ITEM GUI-Item是计算机屏幕上窗口中活动组件的数据表示。

函数

(create-window g)  Window

  g : (listof (listof GUI-ITEM))
用GUI-ITEM的“矩阵”g创建Window。

函数

(window? x)  boolean?

  x : any/c
输入值是window吗?

函数

(show-window w)  true

  w : Window
显示w

函数

(hide-window w)  true

  w : window
隐藏w

函数

(make-button label callback)  GUI-ITEM

  label : string>
  callback : (-> event%  boolean)
label(标签)和callback(回调)函数创建按钮。 回调函数读入一个值,可以安全地忽略之。

函数

(make-message msg)  GUI-ITEM

  msg : string?
msg创建消息item。

函数

(draw-message g m)  true

  g : GUI-ITEM
  m : string?
在消息item g中显示m,同时删除当前消息。

函数

(make-text txt)  GUI-ITEM

  txt : string?
创建标签为txt的文本编辑器,允许用户输入文本。

函数

(text-contents g)  string?

  g : GUI-ITEM
求text GUI-ITEM的当前内容。

函数

(make-choice choices)  GUI-ITEM

  choices : (listof string?)
choices创建选择菜单,允许用户从一些选项中进行选择。

函数

(choice-index g)  natural-number/c

  g : GUI-ITEM
求choice GUI-ITEM当前选中的选项;结果是选择菜单从0开始的索引。

示例1:
> (define w
    (create-window
      (list (list (make-button "QUIT" (lambda (e) (hide-window w)))))))
; 屏幕上出现一个按钮。
; 单击按钮,它将消失。
> (show-window w)
; 窗口消失。

示例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
; 将text1的当前内容绘制到msg1中,前面加上“Hello, ”
(define (respond e)
  (draw-message msg1 (string-append "Hello, " (text-contents text1))))
 
; 设置有三“行”的窗口:
;    text field、message,以及两个按钮
; 填写文本并单击“OKAY”
(define w
  (create-window
   (list
    (list text1)
    (list msg1)
    (list (make-button "OKAY" respond)
          (make-button "QUIT" (lambda (e) (hide-window w)))))))