球拍GUI`计时器%`的奇怪行为

时间:2018-03-25 16:17:27

标签: user-interface racket

我正在尝试编写一个简单的程序,它显示当前时间并每秒更新一次。但是,当我运行下面的代码时

#lang racket/gui

(require racket/date)

(define frame (new frame%
                   [label "Not Functioning Timer"]
                   [width 300]
                   [height 100]))

(new timer%
     [interval 1000]
     [notify-callback (lambda ()
                        ;(display (format "The time is: ~a"
                        ;                 (date->string (current-date) #t)))
                        ;(newline) ; the commented lines work
                        ;; however the behavior of the following lines is weird
                        (send time-msg
                              set-label
                              (format "The time is: ~a"
                                      (date->string (current-date) #t))))])


(define time-msg (new message%
                      [parent frame]
                      [label "nothing so far..."]))

(send frame show #t)

我得到了

program snapshot

缺少涉及当前时间的描述。但是,当我取消注释(display ...)行时,它会产生输出:

(object:timer% ...)
The time is: Monday, March 26th, 2018 12:14:33am
The time is: Monday, March 26th, 2018 12:14:34am
The time is: Monday, March 26th, 2018 12:14:35am
The time is: Monday, March 26th, 2018 12:14:36am
...

运作良好。

我不明白为什么会这样。请帮忙!

顺便说一句,我该怎么做才能将文字对齐到左侧而不是将它们放在中心?

感谢。

1 个答案:

答案 0 :(得分:2)

问题是首次渲染容器时组件的大小。然后你的消息时间太长了。

(define time-msg (new message%
                      [parent frame]
                      [auto-resize #t]
                      [label "nothing so far..."]))

如果您将auto-resize属性添加到#t,您应该看到自己想要的内容。