in reply to Re^2: Tk ::TableMatrix
in thread Tk ::TableMatrix

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

Replies are listed 'Best First'.
Re^4: Tk ::TableMatrix
by lil_v (Sexton) on Jul 02, 2008 at 19:00 UTC
    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?
      First you are not seeing the problem. As a lesson on your code, you have your logic all screwed up. Since you don't reset $i after each inner loop run, you don't cycle thru everything. Furthermore, you assign @array to the same undefined reference each time.... so you get one entry.

      YOUR REAL PROBLEM is you don't understand the data structure involved, it's already there in the hash, all you need to do is pull it out. Read "perldoc perlreftut" and see References quick reference.

      The following code should show you what's up. Don't let the name $arrayVar throw you off, it's actually an hashref for %$arrayVar.

      #!/usr/bin/perl my $arrayVar = {}; # this is actually a hashref 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; } } foreach my $key (sort keys %$arrayVar){ print "$key -> ",$$arrayVar{$key},"\n"; #or # print "$key -> ",$arrayVar->{$key},"\n"; }

      I'm not really a human, but I play one on earth CandyGram for Mongo
Re^4: Tk ::TableMatrix
by lil_v (Sexton) on Jul 15, 2008 at 15:05 UTC
    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' ); }
      If you are going to start modifying rows and cols, you will be better off using a straight TableMatrix, and putting it in a Scrolled Pane. Test for inputs right away, if you want to wait till later to do it, pop up a dialog and ask.
      #!/usr/bin/perl use warnings; use strict; use Tk; require Tk::TableMatrix; require Tk::Pane; # usage programname rows cols my ( $rows, $cols ) = @ARGV; #Then you could test if anything passed: if ( !defined $rows ) { $rows = 4 } if ( !defined $cols ) { $cols = 4 } my $mw = tkinit; $mw->geometry('400x400'); my $tp = $mw->Scrolled('Pane', -sticky => 'nw', )->pack( -fill =>'both', -expand => 1, ); my $bf = $mw->Frame()->pack(); my $t = $tp->TableMatrix( -cols => $cols, -rows => $rows, )->pack(-fill =>'both', -expand => 1 ); $bf->Button(-text=> 'add column', -command=>sub{$t->packPropagate(1); $t->insertCols(0,1); $mw->update; }, )->pack(-side => 'left'); $bf->Button(-text=> 'add row', -command=>sub{$t->packPropagate(1); $t->insertRows(0,1); $mw->update; }, )->pack(-side => 'right'); MainLoop;

      I'm not really a human, but I play one on earth CandyGram for Mongo
        Since the table is user defined, lets say the user makes a mistake so he goes back and sets other values. How do you make the initial table disappear? I tried using destroy() but that just takes off the table completely and doesn't allow it to comeback. Is there another option that just updates the table? For example:
        #!/usr/bin/perl use warnings; use strict; use Tk; require Tk::TableMatrix; require Tk::Pane; my $mw = MainWindow->new; my $arrayVar = {}; my $t; $mw->Button(-text => "Create Table", -command => \&create_table)->pack +(); sub create_table { # usage programname rows cols my ( $rows, $cols ) = @ARGV; #Then you could test if anything passed: if ( !defined $rows ) { $rows = 4 } if ( !defined $cols ) { $cols = 4 } my $mw = tkinit; $mw->geometry('400x400'); my $tp = $mw->Scrolled('Pane', -sticky => 'nw', )->pack( -fill =>'both', -expand => 1, ); my $bf = $mw->Frame()->pack(); my $t = $tp->TableMatrix( -cols => $cols, -rows => $rows, )->pack(-fill =>'both', -expand => 1 ); $bf->Button(-text=> 'add column', -command=>sub{$t->packPropagate(1); $t->insertCols(0,1); $mw->update; }, )->pack(-side => 'left'); $bf->Button(-text=> 'add row', -command=>sub{$t->packPropagate(1); $t->insertRows(0,1); $mw->update; }, )->pack(-side => 'right'); } MainLoop;