如何在 Racket/PLT-Scheme 中检索按钮的标签?
我正在尝试来自 HtDP 的练习 22.3.3 但不知道如何检索被单击按钮的标签。我收到此消息 draw-message: Expected
这似乎表明我需要一个字符串,但我正在获取一个类的实例。答案在回调中吗?如果是这样,我该如何解构它?
这就是我到目前为止所得到的:
(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”,则会调用回调
。但是我如何检索值“9”呢?这是我不确定的。
I'm attempting Exercise 22.3.3 from HtDP but do not know how to retrieve the label of the button that was clicked. I get this message draw-message: expected <string> as second argument, given: (instantiate (class ...) ...)
which seems to suggest that I need a string but I'm getting an instance of a class. Is the answer in the callback? If so, how do I de-structure it?
This is what I have so far:
(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)))
If I understand things correctly, each button will call call-back
when it is pressed. This in turn should call display
which will update the text. However, I do not understand how to retrieve the caller's label. e.g. if the button "9" is pressed, it will call call-back
. But how do I retrieve the value "9"? This is what I'm unsure about.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的。绘制消息函数使用一个“窗口”和一个“字符串”,这在同一部分的图 62 中进行了记录。您似乎将其应用于“按钮对象”。另请参阅同一部分中的示例 2,如下所示:
查看当您单击“立即复制”按钮时 echo-message 如何更改显示。
提示:由于每个按钮都有一个回调,因此您确切地知道从哪个按钮回调将哪个字符串发送到显示器。
更正:书中的示例1已损坏。 代替使用
。
Correct. The draw-message function consumes a 'window' and a 'string', which is documented in figure 62 in the same section. You seem to be applying it to a 'button object'. Also see example 2 in the same section, which looks like this:
See how echo-messsage changes the display when you click the 'copy now' button.
Hint: since you have one callback per button, you know exactly which string to send to the display from which button callback.
Correction: Example 1 in the book is broken. Use
instead.