« GUI design - Sketch it out first! (Java / Swing example) | Main | Oliver says .... »
August 31, 2007
Tktable - Laying out data in a matrix - Tcl/Tk
There's a very common requirement to tabulate data within a GUI, but neither Java (in awt) or Tcl (in Tk) had the facility built in from "day 1". In Java, you now have Jtable within Swing ([example]) and in Tcl you can use a Tktable.
Although Tktable isn't part of standard Tk, you'll find it as a part of most distributions these days, and if it's not in yours, you can download it f.o.c. from Sourceforge. Right - enough of the basics. Here'a an example

Which is displayed from a very simple sample program which we have written - full source code [here] and I then added data within the table as it's interactive. The output produced is as follows when we press the "Save as text" button.
earth-wind-and-fire:~/aug07/tkk grahamellis$ wish tkt
Data is 14 rows by 8 columns
{Melksham - London} 1,1 - 2
{Melksham - Reading} 1,2 - 2
{Melksham - Gloucester} 1,3 - 2
{Melksham - Swindon} 1,4 - 2
{Melksham - Bath} 1,5 - 4
{Melksham - Chippenham} 1,6 - 2
{Melksham - Melksham} 1,7 - -
{Frome - Swindon} 5,4 - Yes
earth-wind-and-fire:~/aug07/tkk grahamellis$
Here are the highlights from that sample program:
package require Tktable
We load in the Tktable optional package as required
array set cells {
0,1 London 0,2 Reading
0,3 Gloucester 0,4 Swindon ...
The cells are populated from a (sparse) array with the element names being row, column pairs
table .mytab -rows 14 -cols 8 -variable cells
pack .mytab
Create and pack a tktable widget. Lots of options can be used, but I've just put a minimal few important ones in here to keep the example short and sweet.
proc SaveTable {} {
global cells;
set Rows [lindex [.mytab configure -rows] end]
set Cols [lindex [.mytab configure -cols] end]
The action command when the "Save" button is pressed; it starts off by grabbing the number of rows and columns (which it then loops through) - and the number is slighly awkwardly on the end of a list returned by the configure option on the tktable widget.
for {set r 1} {$r < $Rows} {incr r} {
set rowname $cells($r,0)
for {set c 1} {$c < $Cols} {incr c} {
set colname $cells(0,$c)
set cellname "$r,$c"
if {![info exists cells($cellname)]} {continue}
puts "{$rowname - $colname} $cellname - $cells($cellname)"
}
}
This is a "classic" loop going through each row and each column and reading the data from the tktable back. You'll note that I used an info exists to ensure that my program didn't crash when I hit a cell that had not been completed.
We cover Tcl programming on our Tcl programming course and then Tk on our Tk Course. Tktable is one of many widgets we introduce you too - there's something of a pattern, so that once I've taught you one, you can learn the others vey easily from the documentation and the knowledge I have imparted. Oh - talking about documentation and further examples, you might like to have a look at a much fuller example published elsewhere
Posted by gje at August 31, 2007 01:26 PM