[plt-scheme] MrEd: how can I _cleanly_ keep the states of various widgets in sync?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Nov 29 19:12:56 EST 2007

You communicate the event of a change in GUI state via a channel and  
then your thread can update the guis. Something like the below is a  
start.

(Since the #t is useless, I am wondering whether we can get some  
other kind of event to do all this, looking a bit more functional.)

-- Matthias


#! /bin/sh
#| Hey Emacs, this is -*-scheme-*- code!
#$Id: gui-constraints.ss 5286 2007-11-29 23:51:36Z erich $
exec mred --no-init-file --mute-banner --version --require "$0"
|#
(module gui-constraints mzscheme
   (require (lib "class.ss")
            (lib "mred.ss" "mred"))

   (define frame (new frame% (label "Constraint Testing")))

   (define delta (make-channel))

   (define button
     (new button% (parent frame) (label "Click me!")
          (callback (lambda (item event)
                      (message-box "OK" "Now what?")))))

   (define radio-box
     (new radio-box% (label "Pick one")
          (callback (lambda (e x) (channel-put delta #t)))
          (choices '("Disable that button there" "Let it be enabled"))
          (parent frame)))

   (thread
    (lambda ()
      (let loop ()
        (sync (system-idle-evt) delta)
        (channel-get delta)
        (send button enable
              (not (zero? (send radio-box get-selection))))
        (loop))))

   (send frame show #t)

   )



Posted on the users mailing list.