Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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

Sample of the TkTable widget

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
(written 2007-08-31, updated 2007-09-04)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T246 - Tcl/Tk - Tix Megawidgets

Back to
GUI design - Sketch it out first! (Java / Swing example)
Previous and next
or
Horse's mouth home
Forward to
Oliver says ....
Some other Articles
MySQL joins revisited
While waiting for Melksham Post Office
Subway Restaurant in Melksham, Wiltshire
Oliver says ....
Tktable - Laying out data in a matrix - Tcl/Tk
GUI design - Sketch it out first! (Java / Swing example)
Java - Client side applet applications as well as server side
Well House Manor appoints a General Manager
Easy handling of errors in PHP
Flash - is it available to your web page?
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1327_Tkt ... cl-Tk.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat