Updated 2016-01-29 13:02:46 by HJG

Summary  edit

Richard Suchenwirth 2003-04-02: Here is a custom dialog - a toplevel that prompts for a value:

Description  edit

proc value_dialog {string} {
    set w [toplevel .[clock seconds]]
    wm resizable $w 0 0
    wm title $w "Value request"
    label  $w.l -text $string
    entry  $w.e -textvar $w -bg white
    bind $w.e <Return> {set done 1}
    button $w.ok     -text OK     -command {set done 1}
    button $w.c      -text Clear  -command "set $w {}"
    button $w.cancel -text Cancel -command "set $w {}; set done 1"
    grid $w.l  -    -        -sticky news
    grid $w.e  -    -        -sticky news
    grid $w.ok $w.c $w.cancel
    vwait done
    destroy $w
    set ::$w
#----- Test
set test [value_dialog "Give me a value please:"]
puts test:$test
pack [ label .l -text "Value: '$test' " ]

MSW: I wouldn't specify the entry's background, for a real reusable dialog, I'd check the option database to see if something exists.

Imho such little snippets should use a global option hierarchy so you can easily integrate them into application which use the option database themselves. In my (standard wish) environment, I get an entry with white text on white background :) Aside of that, nice...

MSW: What is not so nice is that you set result to "" instead of setting $w to "". I've changed that.

RS: Yup - development residue: the global variable now named after the window was called result, which is likely to conflict with other results... Thanks, you fixed it before I got the chance to do it ;-)

HJG 2016-01-28: This is smallest edit-value-dialog I have found on this wiki so far. Nice, small and useful, just as that m+-menu routine.
Nevertheless, I don't understand some details about this code:

  • Why returning the result, when the value is in a global var ?
  • Why use a random name (from clock) for the dialog ?
  • Why blank the value, instead of restore to the old value when pressing cancel ?
  • What is the point of wm resizable here ?

I have some ideas on how to modify this routine, so I'm going to make a fork of this at edit_val.

See Also  edit

Another little value dialog
A not-so-little value dialog