我正在尝试来自 HtDP 的练习 22.3.3,但不知道如何检索被单击按钮的标签。我收到这条消息draw-message: expected <string> as second argument, given: (instantiate (class ...) ...)
,似乎表明我需要一个字符串,但我得到了一个类的实例。回调中有答案吗?如果是这样,我该如何解构它?
这是我到目前为止所拥有的:
(define pad1
'((1 2 3)
(4 5 6)
(7 8 9)
(\# 0 *)))
(define pad2
'((1 2 3 +)
(4 5 6 -)
(7 8 9 *)
(0 = \. /)))
(define (title t)
(make-message t))
(define display
(make-message ""))
(define (pad->gui p)
(cond
[(empty? p) empty]
[else (cons (button-maker (first p))
(pad->gui (rest p)))]))
;; make buttons out of a list
(define (button-maker a-list)
(cond
[(empty? a-list) empty]
[(number? (first a-list))(cons (make-button (number->string (first a-list)) call-back)
(button-maker (rest a-list)))]
[(symbol? (first a-list))(cons (make-button (symbol->string (first a-list)) call-back)
(button-maker (rest a-list)))]))
(define (call-back b)
(draw-message display ...))
(create-window
(append (list (list (title "Virtual Phone")))
(list (list display))
(pad->gui pad1)))
如果我理解正确,每个按钮都会call-back
在按下时调用。这反过来应该调用display
它将更新文本。但是,我不明白如何检索调用者的标签。例如,如果按下按钮“9”,它将调用call-back
. 但是如何检索值“9”?这是我不确定的。