[plt-scheme] Re: Text-contents to a function variable

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Wed Jan 13 15:46:32 EST 2010

On Wed, Jan 13, 2010 at 3:21 PM, Avi <n4alpaca at gmail.com> wrote:
> Stephen, you are right about the defining H as a scheme variable to
> have the value of 1.
> I'm going to answer as clearly as I can so that there is no confusion.
> If anything that is not one of the scheme variables that was defined
> before hand a message will pop up explaining the exact way (for
> simplicity's sake) to use the text-field. (Do you think a giant
> conditional is the way to go?)
>
> I do not believe that the variables will clash because the value that
> we get from the scheme variables will be used for a program that
> checks certain combinations. Any instance where the entered value is
> not the same, would bring up the error message explaining to check
> what you wrote. Would you like to see what I have for my overall code
> so far so you can better understand my problem?

Avi,

Here is some sample code that will accept inputs to a text field from
a fixed list, and print out a corresponding number.  (If you want to
use that number to compute, instead of print, you certainly can, but
this is a simple example.)  For any other input, it prints an error
message.  (Once again, if you want this in a popup window instead of
the main window, you can certainly do that in your program.)  I hope
this is illustrative of how to use a hash table to map from inputs to
outputs, without relying on variable names.  Of course you are welcome
to use variables of the same name, but it is probably best not to rely
on the variable names themselves for runtime computation.

#lang scheme/gui

;; What to do when the user enters a new string:
(define (text-callback t e)
  ;; Wait for the user to press enter
  (when (eq? (send e get-event-type) 'text-field-enter)
    ;; Get the value they entered
    (let ([value (send text get-value)])
      ;; Compute the new message to display
      (send message
            set-label
            ;; Check for a legal value
            (if (hash-has-key? table value)
                ;; If the value is legal, print out its number.
                (format "~a" (hash-ref table value))
                ;; Otherwise, print an error message.
                error-message)))))

;; Things the user can type:
(define legal-inputs
  '("cat" "dog" "bird" "mouse"))

;; Corresponding numbers:
(define legal-outputs
  '(10 20 30 40))

;; Build a table mapping inputs to outputs:
(define table
  (for/hash ([input (in-list legal-inputs)]
             [output (in-list legal-outputs)])
    (values input output)))

;; Construct a helpful error message:
(define error-message
  (format "Enter one of: ~a" legal-inputs))


;; Now create a window with input and output fields.

(define frame
  (new frame%
       [label "The Window"]))

(define text
  (new text-field%
       [parent frame]
       [label "Input:"]
       [callback text-callback]))

(define message
  (new message%
       [parent frame]
       [stretchable-width #t]
       [label error-message]))

;; And finally, open the window!
(send frame show #t)


Posted on the users mailing list.