I've been fooling around with the TkTable widget/dll that came with the 8.3.4.2 download. I have a suspicion that it could be deucedly useful but so far I'm just amusing myself. In case there are others like me I thought I'd include what I've done so far. In particular, I use a lot of ASCII "databases" that are really comma separated value files. I don't want to use a spreadsheet to look at them since that takes longer and seems inelegant. So, I have a routine to populate a table with the contents of the database (or, really, any comma delimited file) and to edit the table (and hence the matrix) and save the result:
#set default filename
set filename tlmdb.csv
#
load "c:/program files/tcl/lib/tktable2.7/tktable.dll"
wm title . "CSV View/Edit"
wm deiconify .
frame .top -borderwidth 2
pack .top -side top
set w .top
label $w.lb -text "CSV file name: "
entry $w.en -textvariable filename -width 42
button $w.bu -text readCSV -command fillTbl
bind $w.en <Return> fillTbl
bind . <Escape> exit
pack $w.lb $w.en -side left
pack $w.bu -side left -padx 12
proc fillTbl {} {
destroy .t .yscr .xscr .f
frame .f -borderwidth 2 -relief groove
pack .f -side bottom
set w .f
button $w.r -text Add_row -command {.t insert rows end 1}
button $w.d -text Del_row -command {.t delete rows [.t index active row] 1}
button $w.s -text Save -command saveM
pack $w.r $w.d $w.s -side left -padx 3 -pady 3
table .t -yscrollcommand ".yscr set" -xscrollcommand ".xscr set"
scrollbar .yscr -command ".t yview"
scrollbar .xscr -command ".t xview" -orient horizontal
pack .yscr -side right -fill y
pack .t -side top
pack .xscr -side top -fill x
.t configure -variable m -bg white -width 12 -height 12
set fid [open $::filename r]
set flst [split [read $fid] \n]
close $fid
set nrows [llength $flst]
.t configure -rows $nrows
.t configure -cols [llength [split [lindex $flst 0] ,]]
catch [array unset ::m] rtn
for {set i 0} {$i<$nrows} {incr i} {
set linen [lindex $flst $i]
set llst [split $linen ,]
foreach elm $llst {
set j [lsearch $llst $elm]
set ::m($i,$j) $elm
}
}
}
proc saveM {} {
global m filename
set fid [open $filename w]
set nrows [.t cget -rows]
set ncols [.t cget -cols]
for {set i 0} {$i<$nrows} {incr i} {
set rwlst ""
for {set j 0} {$j<$ncols} {incr j} {
if [catch {lappend rwlst $m($i,$j)} rtn] {lappend rwlst " "}
}
set ostring [join $rwlst ,]
puts $fid $ostring
}
close $fid
}
As I said, this is really self-amusement so far but I have a feeling it will come in handy soon. Bob Rashkin
rrashkin@csc.com
#set default filename
set filename tlmdb.csv
#
load "c:/program files/tcl/lib/tktable2.7/tktable.dll"
wm title . "CSV View/Edit"
wm deiconify .
frame .top -borderwidth 2
pack .top -side top
set w .top
label $w.lb -text "CSV file name: "
entry $w.en -textvariable filename -width 42
button $w.bu -text readCSV -command fillTbl
bind $w.en <Return> fillTbl
bind . <Escape> exit
pack $w.lb $w.en -side left
pack $w.bu -side left -padx 12
proc fillTbl {} {
destroy .t .yscr .xscr .f
frame .f -borderwidth 2 -relief groove
pack .f -side bottom
set w .f
button $w.r -text Add_row -command {.t insert rows end 1}
button $w.d -text Del_row -command {.t delete rows [.t index active row] 1}
button $w.s -text Save -command saveM
pack $w.r $w.d $w.s -side left -padx 3 -pady 3
table .t -yscrollcommand ".yscr set" -xscrollcommand ".xscr set"
scrollbar .yscr -command ".t yview"
scrollbar .xscr -command ".t xview" -orient horizontal
pack .yscr -side right -fill y
pack .t -side top
pack .xscr -side top -fill x
.t configure -variable m -bg white -width 12 -height 12
set fid [open $::filename r]
set flst [split [read $fid] \n]
close $fid
set nrows [llength $flst]
.t configure -rows $nrows
.t configure -cols [llength [split [lindex $flst 0] ,]]
catch [array unset ::m] rtn
for {set i 0} {$i<$nrows} {incr i} {
set linen [lindex $flst $i]
set llst [split $linen ,]
foreach elm $llst {
set j [lsearch $llst $elm]
set ::m($i,$j) $elm
}
}
}
proc saveM {} {
global m filename
set fid [open $filename w]
set nrows [.t cget -rows]
set ncols [.t cget -cols]
for {set i 0} {$i<$nrows} {incr i} {
set rwlst ""
for {set j 0} {$j<$ncols} {incr j} {
if [catch {lappend rwlst $m($i,$j)} rtn] {lappend rwlst " "}
}
set ostring [join $rwlst ,]
puts $fid $ostring
}
close $fid
}
As I said, this is really self-amusement so far but I have a feeling it will come in handy soon. Bob Rashkin
rrashkin@csc.com