如何在 Racket/PLT-Scheme 中检索按钮的标签?

发布于 2024-10-10 18:11:42 字数 1468 浏览 5 评论 0原文

我正在尝试来自 HtDP 的练习 22.3.3 但不知道如何检索被单击按钮的标签。我收到此消息 draw-message: Expected;作为第二个参数,给出: (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”,则会调用回调。但是我如何检索值“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-10-17 18:11:42

正确的。绘制消息函数使用一个“窗口”和一个“字符串”,这在同一部分的图 62 中进行了记录。您似乎将其应用于“按钮对象”。另请参阅同一部分中的示例 2,如下所示:

(define a-text-field
  (make-text "Enter Text:"))

(define a-message
  (make-message "`Hello World' is a silly program."))

(define (echo-message e)
  (draw-message a-message (text-contents a-text-field)))

(define w (create-window
           (list (list a-text-field a-message)
                 (list (make-button "Copy Now" echo-message)))))

查看当您单击“立即复制”按钮时 echo-message 如何更改显示。

提示:由于每个按钮都有一个回调,因此您确切地知道从哪个按钮回调将哪个字符串发送到显示器。

更正:书中的示例1已损坏。 代替使用

 (define u
  (create-window (list (list (make-button "Close" (lambda (x) (hide-window u)))))))

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:

(define a-text-field
  (make-text "Enter Text:"))

(define a-message
  (make-message "`Hello World' is a silly program."))

(define (echo-message e)
  (draw-message a-message (text-contents a-text-field)))

(define w (create-window
           (list (list a-text-field a-message)
                 (list (make-button "Copy Now" echo-message)))))

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

 (define u
  (create-window (list (list (make-button "Close" (lambda (x) (hide-window u)))))))

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文