Arjen Markus (1 july 2005) I am not sure whether the name "properties editor" is the official name for a widget like this, but given the context in which I have seen it in use, I'd say it is pretty close. The idea is that some entity in a program has a bunch of properties and rather than designing a specific dialogue/window to view and change the values of these properties, you simply use a generic one. The only thing you need as a programmer is the names of these properties, supposedly descriptive and informative to the actual user, and the types of these properties, an arbitrary string, a file name, ...
So, when I had seen this type of widget for the n'th time, I thought: I can do this in Tk, can't I? The script below is the result of about half an evening at the keyboard and screen. It is not perfect, it does not classify as a (meta-)widget, but in principle it is useful.
Enjoy and improve!
(Oh, yes, some statements are more complicated than they need to be, but they are internal, so I do not care too much :))
Things to improve:
Things puzzling:
# properties.tcl -- # Simple properties editor # # PropertiesColor -- # Select a new color # # Arguments: # wcolor Name of the label that gets the color # var Name of the variable associated with it # Result: # None # proc PropertiesColor { wcolor var } { set color [uplevel #0 [list set $var]] set color [tk_chooseColor -initialcolor $color -parent $wcolor] if { $color != "" } { $wcolor configure -bg $color uplevel #0 [list set $var $color] } } # PropertiesShow -- # Show or hide a frame # # Arguments: # wcheck Name of the checkbutton # wframe Name of the frame # Result: # None # proc PropertiesShow { wcheck wframe row } { set var [$wcheck cget -variable] if { [uplevel #0 [list set $var]] == 1 } { grid configure $wframe -row $row -sticky news } else { grid forget $wframe } } # propertiesEditor -- # Create a new window to edit properties # # Arguments: # widget Widget name # properties Definition of the properties # valuesName Name of a (global) variable containing the values # Result: # The widget name # proc propertiesEditor { widget properties valuesName } { upvar #0 $valuesName values set group 0 set row 0 set count 0 set w [frame $widget] set wg $w foreach {type name} $properties { switch -- $type { "group" { set values($name) 1 set wt [checkbutton $widget.t$group -text $name \ -variable "${valuesName}($name)" \ -offrelief flat \ -overrelief flat ] set wg [frame $widget.w$group -relief solid -bd 2 \ -bg white] grid $wt -sticky w grid $wg -sticky news incr group incr row $wt configure -command [list PropertiesShow $wt $wg $row] incr row set count 0 } "string" { set wl [label $wg.l$count -text $name -fg black -bg white] set we [entry $wg.e$count -textvariable "${valuesName}($name)" ] grid $wl $we - -sticky w incr count } "logical" { #set wc [checkbutton $wg.c$count -text $name \ # -variable "${valuesName}($name)" -justify left] #set dummy [label $wg.l$count -text " " ] #grid $wc $dummy -sticky news ;# Stickiness does not work as I expected set wl [label $wg.l$count -text $name \ -fg black -bg white] set ws [spinbox $wg.s$count -values "True False" \ -textvariable "${valuesName}($name)" \ -fg black -bg white] grid $wl $ws - -sticky w incr count } "color" { set wl [label $wg.l$count -text $name -fg black -bg white] set wc [label $wg.c$count -text \ [string repeat " " 20] -bg $values($name)] set wb [button $wg.b$count -text "Choose" \ -command [list PropertiesColor $wc "${valuesName}($name)"]] grid $wl $wc $wb -sticky w incr count } } } return $w } # main -- # Testing the above procedures # set valuesx(name) "My name" set valuesx(address) "My address" set valuesx(value) 0 set valuesx(myfavourite) red set props {group Personalia string name string address group Data logical value color myfavourite } set w [propertiesEditor .w $props valuesx] pack $w -fill both
TV jul 19 05 Perhaps point to LemonTree.