2

因此,我正在尝试编写一个同时使用big-bang(参见2htdp/universe)函数和对话框(参见racket/gui/base)的程序。我的问题是我想要它以便程序同时显示两个窗口但是我很难弄清楚这部分,因为两个函数必须“关闭/完成”才能继续代码。这是我尝试过的,没有运气(由于之前所说的):

#lang racket

(require 2htdp/universe
         racket/gui/base)

(define dialog (instantiate dialog% ("Title")))
(define (render data)
   ...)

(define main
   (begin
      (big-bang ...
         (on-draw render))
      (send dialog show #t)))

使用此示例 [模板] 代码,big-bang 应用程序首先显示,为了显示对话框,您必须关闭 big-bang 应用程序。重申一下,我希望能够同时显示它们。

如果您想了解有关该问题的更多信息,请告诉我。提前感谢您的帮助。

4

1 回答 1

4

您可以在具有自己的事件空间的单独线程中运行这两个线程,这样可以防止它们阻塞彼此的执行。你期待两人之间会有交流吗?

这是一个例子:

#lang racket

(require 2htdp/universe
         2htdp/image
         racket/gui/base)

;; start-dialog: -> void
(define (start-dialog)
  (define dialog (new dialog% [label "A sample title"]))
  (send dialog show #t))

;; start-up-big-bang: -> number
(define (start-up-big-bang)
  (big-bang 0
            (on-tick add1 1)
            (to-draw draw)))

;; draw: number -> image
(define (draw w)
  (text (format "I see my number is: ~a" w) 20 "black"))

;; We create fresh eventspaces for each.
;; See: http://docs.racket-lang.org/gui/windowing-overview.html#(part._eventspaceinfo)
;; for more details.
(define c1 (make-eventspace))
(define c2 (make-eventspace))

;; And now we can spawn off these two to run concurrently.
(define t1 (parameterize ([current-eventspace c1])
             (thread start-up-big-bang)))
(define t2 (parameterize ([current-eventspace c2])
             (thread start-dialog)))

;; Let's wait till both the big-bang and the dialog have closed.
(thread-wait t1)
(thread-wait t2)
于 2012-04-12T04:07:09.457 回答