所以我正在尝试编写一个同时使用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应用程序。为了重新迭代,我希望能够同时显示它们。
如果您想了解有关此问题的更多信息,请告知我们。提前感谢您的帮助。
答案 0 :(得分: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)