''How can I traverse the object tree to assemble an array of object names and values of my widgets?"
abstract: Looking for a good way to allow users to save / restore values of text boxes, radio buttons, and combo boxes.
motivation: My application will have 'hundreds' of text boxes filled with binary data. Users will manually enter the data one bit at a time. They will want to enter the data 'one time' and then load it for use many times thereafter.
strategy: I am new to Tcl, so I do not know the 'best practice' for accomplishing this task. Here is my idea: Thanks to Roy Terry for his advice so far, and his 'wfind' procedure.
I see a number of items marked done. What specifically is keeping you from finishing?
RTsave 1) Create save event button, to select a save file name (DONE) 2) On save (save_all_to_file) event, iterate through the object parent-child heirarchy to capture object names, and values. Assemble these value pairs into an array. 3) Save array to file.
restore 1) Create load event button, enable file select (DONE) 2) On load (load_all_from_file) event, source the array in the file. 3) Iterate through array and set widget values throughout the application.
#############################################################################
## Procedure: save_all_to_file
proc ::save_all_to_file {} {
global widget
set types {
{{Save Files} {.save} }
}
set filename [tk_getSaveFile -filetypes $types]
if {$filename != ""} {
### HERE I DO A MANUAL ARRAY CREATION--WANT TO FIGURE OUT HOW TO AUTOMATE THIS STEP FOR ALL APPROPRIATE WIDGETS###
### I want to use something like the proc 'wfind' (below)
array set myGlobals [list {cbSPC0} [Toplevel1 setvar cbSPC0] {cbSPC1} [Toplevel1 setvar cbSPC1] {cbSPC2} [Toplevel1 setvar cbSPC2] ]
saveState $filename.save myGlobals
}
}
#############################################################################
## Procedure: saveState
proc ::saveState {stateFile stateArray} {
global widget
upvar $stateArray state
set output [open $stateFile "w"]
puts $output "global $stateArray \n [list array set $stateArray [array get state]]"
close $output
}
#############################################################################
## Procedure: wfind: Use something like this to iterate the object tree.
## don't yet know how to leverage this to get object name / value pairs assembled into an
## array.
proc ::wfind {{win .}} {
set out $win
foreach w [lsort -dictionary [winfo children $win]] {
lappend out " [wfind $w]"
} return $out
}
#############################################################################
## Procedure: load_all_from_file
proc ::load_all_from_file {} {
global widget
set types {
{{Save Files} {.save} }
}
set filename [tk_getOpenFile -filetypes $types]
if {$filename != ""} {
source $filename
}
foreach id [array names myGlobals] {
Toplevel1 setvar $id $myGlobals($id)
}
}
Thank you for any advice you might be able to offer!
Kevin Ready kready@taec.toshiba.com Toshiba America Electronic Components, STI Design Center Austin Texas
AF says see
serializing for more info
---