2

So I am trying to write a program that uses both the big-bang (see 2htdp/universe) function as well as dialogs (see racket/gui/base). My problem is that I want it so that the program displays both of the windows at the same time, however I am having a hard time figuring that part out, as both functions must be "closed/completed" for code to continue. This is what I have tried, with no luck (due to what was said previously):

#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)))

With this sample [template] code, the big-bang application shows first, and for the dialog to show, you must close the big-bang application. To re-iterate, I want to be able to have both of them show at the same time.

If you would like any more information about the problem, please let me know. Thanks in advance for any help.

Alex Brooks
  • 1,151
  • 1
  • 10
  • 39

1 Answers1

4

You can run both of these in separate threads with their own eventspaces, which should keep them from blocking each other's execution. Do you expect that there will be communication between the two?

Here's an example:

#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)
dyoo
  • 11,795
  • 1
  • 34
  • 44
  • Makes sense. I am not quite sure if I will have them communicating directly yet or not, but it is very probable. If this is the case, would there be any issues using this method? – Alex Brooks Apr 12 '12 at 04:12
  • 1
    I have to admit that I'm not quite sure yet how to make big-bang universe programs talk with racket/gui programs: their intended use is as beginner-friendly simple standalone apps. If you need anything more substantial, staying within just racket/gui programs seems more flexible. You can get something that looks world-like in racket/gui. See: http://stackoverflow.com/questions/8402218/how-to-make-a-gui-using-lisp-drscheme-or-common-lisp/8468211#8468211 for an example. – dyoo Apr 12 '12 at 17:30
  • Okay. This is my first time messing with `racket/gui`, so I was using big-bang simply because I had experience with it. If I can achieve the same goal (being able to draw plots (with `plot`) and lines/other shapes) with solely racket/gui programs, then I will definitely use them instead. – Alex Brooks Apr 12 '12 at 19:08
  • 1
    Yes, you should be able to draw images coming from the `2htdp/image library` as well as from PLoT, into the canvases of `racket/gui`. Use the `render-image` function from `mrlib/image-core` http://docs.racket-lang.org/mrlib/Image_Core.html to do this. – dyoo Apr 12 '12 at 20:14