Re: querying user for string input

From: Brian Denheyer <>
Date: Tue, 7 Sep 1999 21:06:35 -0700 (PDT)

>>>>> "Kaveh" == Kaveh Kardan <> writes:

  Kaveh> What is the simplest way of presenting a user with a dialog and asking
  Kaveh> for a string input?

Why use the simplest when you can just use a routine that's already
been written. :-)


Notice the really ugly define hack at the end of the module. It does
not feel like the right thing to do. Comments on the right thing to
do will be appreciated.



;; Implements a dialog box which is used for information.
(require "Tk-classes")

;; Use modules - this allows the *dialog* variable to be "static". Of
;; course if two dialogs are being used at the same time there'll be a
;; problem. But at least *dialog* won't be in the global name-space.
;; I guess that's why you should be allowed to instantiate modules.

(define-module dialog
  (import STklos STklos+Tk Tk)

  (export get-input)

  (define *dialog* #f)

  (define-class <dialog> ()
    ((top :accessor top)
     (ok-button :accessor ok-button)
     (cancel-button :accessor cancel-button)
     (lentry :accessor lentry)))

  (define make-input-dialog
    (lambda (title label)
      (let ((self (make <dialog>)))
        (set! (top self) (make <toplevel>
                           :title title))
        (let ((top (top self)))

          (set! (ok-button self) (make <button>
                                   :parent top
                                   :text "OK"
                                   :border-width 4
                                   :relief 'raised
                                   :command (lambda () (set! *dialog* #t))))

          (set! (cancel-button self) (make <button>
                                       :parent top
                                       :text "Cancel"
                                       :command (lambda ()
                                                  (set! *dialog* 'cancel))))
          (set! (lentry self) (make <labeled-entry>
                                :parent top
                                :title label))

          (pack (lentry self) :side 'top)
          (pack (ok-button self) :side 'left)
          (pack (cancel-button self) :side 'left)
          ;; return gets you out.
          (bind top "<Return>" (lambda ()
                                 (flash (ok-button self))
                                 (set! *dialog* #t)))
          (focus (entry-of (lentry self)))

  (define wait-for
    (lambda (dialog)
      (tkwait 'variable '*dialog*)
       ((eq? *dialog* #t)
        (value (lentry dialog)))
       ((eq? *dialog* 'cancel)

  (define get-input
    (lambda (title label)
      (let* ((d (make-input-dialog title label))
             (result (wait-for d)))
        (destroy (top d))

;; some kind of ugly hack to put get-input into the "global"
;; environment.

(import dialog)
(define get-input (with-module dialog get-input))

(provide "input-dialog")
Received on Wed Sep 08 1999 - 06:10:57 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST