首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户单击其中的几个按钮时,如何隐藏所有按钮,并给用户一个新的按钮列表,以便在球拍中单击。

当用户单击其中的几个按钮时,如何隐藏所有按钮,并给用户一个新的按钮列表,以便在球拍中单击。
EN

Stack Overflow用户
提问于 2017-04-23 18:57:10
回答 1查看 151关注 0票数 0

我正在写一个程序,我想让用户点击我给他们的3个选项中的一些,然后在点击其中两个之后,我希望程序隐藏所有的选项,并给他们一个用户可以点击的新的选项列表(按钮)。这就是我的代码:

代码语言:javascript
复制
(define frame (new frame% [label "Choices in life"]
                   [width 460]
                   [height 460]))

(define canvas (new canvas% [parent frame]
                    [paint-callback
                     (lambda (canvas dc)
                       (send dc set-scale 3 3)
                       (send dc set-text-foreground red)
                       (send dc draw-text "The choice is yours" 19 30)
                       (send canvas set-canvas-background "black"))]))

告诉你从三种选择中选择两种的信息

代码语言:javascript
复制
(define search-company-msg (new message%
                                [label "Pick two of three:"]
                                [parent frame]
                                ))

一个应该隐藏按钮的函数(我想让它在用户从3种选择中选择2种之后立即隐藏所有按钮。我知道它没有得到正确的实现,但这只是我的一个想法:

代码语言:javascript
复制
(define hide-choice-1-2-3
  (lambda (button event)
    (send button style '(deleted))))

#choice 1

    (define choice-1
      (new button%
           [parent frame]
           [label "choice-1"]
           [min-width 200]
           [min-height 10]
           [callback (lambda (button event)
                (send button enable #f)
                       )]
           ))
#choice 2

(define choice-2
      (new button%
           [parent frame]
           [label "choice-1"]
           [min-width 200]
           [min-height 10]
           [callback (lambda (button event)
                (send button enable #f)
                       )]
           ))
#choice 3

(define choice-3
      (new button%
           [parent frame]
           [label "choice-1"]
           [min-width 200]
           [min-height 10]
           [callback (lambda (button event)
                (send button enable #f)
                       )]
           ))

这些都是我想要出现的新选择,在两种选择被选中之后,旧的选择消失了:

代码语言:javascript
复制
(define choice-4
          (new button%
               [parent frame]
               [label "choice-1"]
               [min-width 200]
               [min-height 10]
               [callback (lambda (button event)
                    (send button enable #f)
                           )]
               ))

(define choice-5
          (new button%
               [parent frame]
               [label "choice-1"]
               [min-width 200]
               [min-height 10]
               [callback (lambda (button event)
                    (send button enable #f)
                           )]
               ))

显示帧

代码语言:javascript
复制
(send frame show #t)

我知道[style '(deleted)]会隐藏按钮,但我不知道如何使用它来隐藏所有的按钮,因为它们中只有几个是被选中的。我的另一个问题是通过替换旧按钮使新按钮出现在框架中。我知道文档中有一个add-child method,但我不知道如何使用它。有人能帮我处理密码吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-23 21:00:36

我不清楚您的总体目标是什么,我不想做很多假设,但是您要寻找的是实现area-container<%>接口的类的area-container<%>方法。样式'deleted用于使小部件在创建时不可见或由容器管理;否则,您将不直接处理它。在任何情况下,简单地添加新按钮都会使它们出现。例如:

代码语言:javascript
复制
#lang racket/gui
(define main-frame (new frame% (label "Main Frame")))

(define button-V
  (new vertical-panel% (parent main-frame)))

(define make-button
  (let ((used-index 0))
    (λ()
      (define b
        (new button%
             (parent button-V)
             (label (format "Button ~a" used-index))
             (callback (λ(b e)
                         (send button-V delete-child b)
                         (make-button)))))
        (set! used-index (add1 used-index))
        b)))

(for ((i (in-range 3)))
  (make-button))

(send main-frame show #t)

根据您的目标,您可能需要更明确地管理孩子。为此,您将在按钮实例化上使用'deleted样式,然后确定要通过add-childchange-children显示哪些按钮,如

代码语言:javascript
复制
(define B
    (new button%
         (parent button-V)
         (label "Mystery")
         (style '(deleted))))
; later... 
(send button-V add-child B)

编辑:,我有一个完整的例子给您。很抱歉,我花了这么长时间来履行我的承诺,但我不确定能否找到一个相当简短和合理明确的例子。我不知道我是否实现了那个目标。

主要策略是: 1)列出选择列表;2)索引它们;3)将它们传递给通过消费列表中的第一组选择而生成按钮的过程;4)这些按钮可以在(3)中调用相同的过程,并与列表的其余部分一起调用。作为额外的奖励,我使这个过程能够循环和记录所做的选择。

我没有声称这是一个很好的方法,但这是我想到的一个解决办法。您的问题的主要问题是,它实际上取决于您想要对选择做什么,因为回答这样的问题几乎肯定会对各种数据结构产生影响。

代码语言:javascript
复制
#lang racket/gui

(define main-frame
  (new frame% (label "Choose Your Fate")
       (width 250)))

(define button-panel
  (new vertical-panel% (parent main-frame)))

(define (clear-children)
  (send button-panel change-children (λ(x) '())))

; ((a b ...) ...) -> ((0 a b ...) (1 ...) ...)
(define (index-choices list-of-choices)
  (for/list ((choice list-of-choices)
             (index (in-naturals)))
    (cons index choice)))

(define choices
  (index-choices '((hello)
                   (yes no)
                   (one two three)
                   (left right up down))))

; all choices are done
; display the choices and offer a restart
(define (finish-choices)
  (clear-children)
  (new message% (parent button-panel)
       (label (responses->string responses)))
  (new button% (parent button-panel)
       (label "Restart")
       (callback (λ(b e)
                   (make-choice-buttons choices)))))


; ((index choices ...) rest...) -> index + (choices ...) (rest...)
(define (unpack choices)
  (let ((head (first choices)))
    (values (first head) (rest head) (rest choices))))

; #(symbols ...)
(define responses (build-vector (length choices) (λ(x) #f)))

; #(symbols ...) -> "symbols ..."
(define (responses->string res)
  (string-join
   (for/list ((val (in-vector res)))
     (symbol->string val))))

(define (make-callback index choice rest)
  (define (call button event)
    ;grab the response
    (vector-set! responses index choice)
    ; add next choices, or finish
    (if (empty? rest)
        (finish-choices)
        (make-choice-buttons rest)))
  call)
; ((index choices ...) rest...) -> buttons per choice
(define (make-choice-buttons choices)
  (define-values (this-index this-choice remaining-choices)
    (unpack choices))
  (clear-children)
  (for ((c this-choice))
    (new button%
         (parent button-panel)
         (label (symbol->string c))
         (callback (make-callback this-index c remaining-choices)))))

(make-choice-buttons choices)
(send main-frame show #t)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43575223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档