ravishi has asked for the wisdom of the Perl Monks concerning the following question:

I am using Tk::TableMatrix and Perl to create a GUI displaying a big table. I have information for usually only one row per column, sometimes two rows. Here is a sample code which shows what I am currently doing.
#!/usr/bin/perl use Tk; use Tk::TableMatrix; use strict; use warnings; my $mw = MainWindow->new; my $table = $mw->Scrolled("TableMatrix", -resizeborders=>'non +e', -titlecols => 1, -rows => 6, -colstretchmode=>'al +l', -cols => 1, -cache => 1, -scrollbars => "osoe +"); for(my $col = 1; $col < 500; $col++) { $table->insertCols("$col.0", 1); my $button = $table->Button(-text => "$col enabled", -command +=> sub {$table->see("0,300"); + } ); #initialy fill table with blank windows. Section to improve my $blank = $table->Label(-text => "", -background => 'white', + -cursor => ['left_ptr']); $table->windowConfigure("0,$col", -window => $blank, -sticky = +> 'nsew'); $blank = $table->Label(-text => "", -background => 'white', -c +ursor => ['left_ptr']); $table->windowConfigure("1,$col", -window => $blank, -sticky = +> 'nsew'); $blank = $table->Label(-text => "", -background => 'white', -c +ursor => ['left_ptr']); $table->windowConfigure("2,$col", -window => $blank, -sticky = +> 'nsew'); $blank = $table->Label(-text => "", -background => 'white', -c +ursor => ['left_ptr']); $table->windowConfigure("3,$col", -window => $blank, -sticky = +> 'nsew'); $blank = $table->Label(-text => "", -background => 'white', -c +ursor => ['left_ptr']); $table->windowConfigure("4,$col", -window => $blank, -sticky = +> 'nsew'); $blank = $table->Label(-text => "", -background => 'white', -c +ursor => ['left_ptr']); $table->windowConfigure("5,$col", -window => $blank, -sticky = +> 'nsew'); #fill each column with a button with random row placement my $row = int(rand(5)); $table->windowConfigure("$row,$col", -window => $button, -stic +ky => 'nsew'); } #set row titles $table->set("0,0", "Col 1"); $table->set("1,0", "Col 2"); $table->set("2,0", "Col 3"); $table->set("3,0", "Col 4"); $table->set("4,0", "Col 5"); $table->set("5,0", "Col 6"); $table->pack(-expand => 1, -fill => 'both'); MainLoop;
Is there a more efficient way of setting every cell to just a blank window and not an editable field. I looked in the perldoc pages and tried using -state => disabled but that didn't seem to let me use -insertRows and -insertCols.

I have found the TableMatrix module to be the best module for what I want compared to Tk::Table, Tk::Grid, and Tk::Hlist so I would not like to switch modules.

Replies are listed 'Best First'.
Re: Non editable cells in Tk::TableMatrix
by zentara (Cardinal) on Oct 03, 2008 at 19:22 UTC
    Here is how I would do it using tags. Tags are kind of tricky ( notice I needed the tagRaise to make the example below work). But this is much faster, because it dosn't make all those Label and Button widgets. Tags are used in the Canvas, Text, and a few other widgets, they are very powerful if used correctly. You should read the perldoc Tk::TableMatrix section on tags, and play with them. To really make them work, you usually need to read the tags on the row,column or cell, then delete out a tag temporarily, then put it back, etc,etc....it can be a real juggling act, but it is simple once you get a scheme going.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      As always zentara, you've come to my rescue. I'm going to have to learn more about tags now. Thanks for the coding format suggestions as well.
      Has anyone ever used row tags with TableMatrix?
        Row tags are pretty straightforward, and work almost like regular tags, except they affect all columns. What sort of thing are you trying to accomplish? Here is a simple example.
        #!/usr/bin/perl use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (10, 10); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $arrayVar->{"$row,$col"} = "$row,$col"; } } print "Creating Table...\n"; ## Test out the use of a callback to define tags on rows and columns sub colSub{ my $col = shift; return "OddCol" if( $col > 0 && $col%2) ; } my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 6, -height => 6, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $mw->Button(-text => "Update", -command => \&update_table) ->pack(-side => 'bottom',-anchor => 'w'); # hideous Color definitions here: $t->tagConfigure('active', -bg => 'white', -relief => 'sunken'); $t->tagConfigure('OddCol', -bg => 'lightyellow', -fg => 'black'); $t->tagConfigure('title', -bg => 'lightblue', -fg => 'black', -relief +=> 'sunken'); $t->tagConfigure('dis', -state => 'disabled'); $t->pack(-expand => 1, -fill => 'both'); $t->focus; Tk::MainLoop; # $t->tagConfigure($anchor, -anchor => $anchor); # $t->tagRow($anchor, ++$i); # $t->set( "$i,$first",$anchor); sub update_table { print "1\n"; $t->tagCell('dis', '1,1' ); $t->activate('2,2'); $t->tagRow('active',3); # $t->configure(-padx =>( $t->cget(-padx))); # a trick needed sometimes to update }

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Non editable cells in Tk::TableMatrix
by zentara (Cardinal) on Oct 03, 2008 at 18:01 UTC
    If you want to use a blank window, instead of an empty-colored cell, I don't think you can do much else than what you are doing now. Creating a window is needed for each and every cell. All I can offer is some code syntax improvements. One is to use the variable option to build your data, this can be useful in importing alot of data. The second is to loop thru the table only once, by nested looping. My code dosn't seem any faster than yours, but it's easier to read and see what is happening. Your big slowdown is in creating all those Labels and Button widgets.

    If you are not stuck on the idea of the nice window look, you could use the tag mechanism to make a disabled tag, and color it someway.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Non editable cells in Tk::TableMatrix
by sflitman (Hermit) on Oct 03, 2008 at 19:29 UTC
    I keep getting this error when I try your code on my ActiveState 5.8.7 installation on WinXP. I downloaded the latest 5.8.8 and it still happens. Maybe you're running under 5.10??
    Had to create Tk::XlibVtab unexpectedly at C:/Perl/lib/DynaLoader.pm l +ine 250.
    My guess is you would do better to tweak the defaults for TableMatrix so it comes up blank first. SSF
      I guess ActiveState TableMatrix win32 has a bug. Search groups.google.com for "XlibVtab unexpectedly". The urls are very long, but here is the deal:

      Will Parsons wrote: > I'm using ActiveState Perl on WinXP. I installed Tk-TableMatrix via + ppm, > but attempting to use it causes errors: Tk-TableMatrix ppm from AS is broken with Tk-804. As Pit suggested tod +ay: Install the package from http://www.bribes.org/perl/ppm/

      I'm not really a human, but I play one on earth Remember How Lucky You Are