in reply to Tk ::TableMatrix

Your code didn't run for me, so I provided a similar working example. Your biggest mistake is
$arrayVar->{"7..9,$col"}->tagConfigure(-state => 'normal'); # you are trying to call a method from a cell...won't work # it must be on the table $t->tagConfigure(........); # read the docs
A working example, the syntax is very finicky, read the docs and experiment until you get it.
#!/usr/bin/perl use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (4, 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'); # 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 scrollbars # if adding columns or rows }

I'm not really a human, but I play one on earth CandyGram for Mongo

Replies are listed 'Best First'.
Re^2: Tk ::TableMatrix
by lil_v (Sexton) on Jul 01, 2008 at 18:28 UTC
    thx u...I needed a good working example. I stil want to know two things, is it poosible to link one of the titles to a pop up window? The other question is that, in the sub update_table, I wanted an if statement that would analyze one row and disable the cells beneath the row depending on their values. For example,
    foreach my $col (1..4){ if ($arrayVar->{"6,$col"} == 1) { $t->tagCell('dis','7,$col'); $t->tagCell('dis','8,$col'); $t->tagCell('dis','9,$col'); } }
    This code doesn't work and i've tried several variations to it. I'm running out of luck...
      I'm not terribly good with TableMatrix, but you could add a browsecmd, and test the column or row, and popup if your col,row condition is met. I just tested this and it seems the titles take the 0 row and 0 column. So you could test if the click is on a title, by detecting a 0 row or column.
      # table option -browsecommand => \&brscmd, sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; print "r$row c$col\n"; # popup if row,col meets some criteria }
      you can do the disabling too, but it is a little tricky, because you have to raise the 'dis' tag over the 'OddCol' tag. You don't have this problem if you don't use the -coltagcommand option.
      #!/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"} = 2*$row + 3*$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 => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $mw->Button(-text => "Update", -command => \&update_table) ->pack(-side => 'bottom',-anchor => 'w'); $mw->Button(-text => "Disable", -command => \&dis_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', -bg => 'black'); $t->pack(-expand => 1, -fill => 'both'); $t->focus; Tk::MainLoop; sub update_table { print "1\n"; $t->tagCell('dis', '1,1' ); $t->tagRaise('dis', 'OddCol'); # only needed if coltagcommand used $t->activate('2,2'); $t->tagRow('active',3); # $t->configure(-padx =>( $t->cget(-padx))); # a trick needed sometimes to update } sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; print "r$row c$col\n"; } sub dis_table{ print "2\n"; foreach my $col (1..4){ if ($arrayVar->{"6,$col"} >= 1) { print "col->$col\n"; $t->tagCell('dis',"7,$col"); $t->tagCell('dis',"8,$col"); $t->tagCell('dis',"9,$col"); } } $t->tagRaise('dis', 'OddCol'); # only needed if coltagcommand used }

      I'm not really a human, but I play one on earth CandyGram for Mongo
        thx alot... it helped me out a lot! Now, I'm faced with the challenge of putting all the datas into an array and print out the array.
        for ($j=1, $j <= 10, $j++){ for ($i=1, $i <= 9, $i++){ @array = $arrayVar->{"$i,$j"}; } } print @array;
        I only get one number when the output is printed, any ideas?
        Hey, I was trying to make the table interactive so the user enters the number of rows and columns needed. It gives me an errory when i try  $rows = <stdin>;. It says that the rows aren't numeric. Is there any way to make the table interactive? I also tried to add an extra row or an extra column with a click of a button but that seems to fail as well. Any suggestions? Here's my code for adding an extra row:
        #!/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 (1..($rows-1)){ foreach my $col (0..($cols-1)){ $arrayVar->{"$row,$col"} = 2*$row + 3*$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 => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $mw->Button(-text => "Add", -command => \&add_row) ->pack(-side => 'bottom',-anchor => 'w'); sub add_row { ++$x; my ($rows,$cols) = (10+$x, 10); foreach my $row (1..($rows-1)){ foreach my $col (0..($cols-1)){ $arrayVar->{"$row,$col"} = 2*$row + 3*$col; } } my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); }