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

I am using Perl and Tk to create a GUI. I have been using Tk::Table to display a table which I found to be better than than Tk::Grid. I have now seen what Tk::TableMatrix can do and I would like to migrate to using that. However, I am currently using buttons in cells in Tk::Table because I can create menus for each button and also bind things fairly easy. Therefore, I would like to continue using buttons in TkTableMatrix.

I have only been able to find a couple of full Tk::TableMatrix examples and they only display editable text in each cell. Is that the only type that is allowed. I have read through the perldoc page and also did some testing myself. Here is what I tried:

#!/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' ); #---------------------------------- #HERE IS WHERE I TRY TO USE BUTTONS #---------------------------------- my $buttonTest = $mw->Button(-text => "test"); $t->set("3,3", $buttonTest); #---------------------------------- $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 }

Replies are listed 'Best First'.
Re: Using Buttons in a cell using Tk::TableMatrix
by zentara (Cardinal) on Sep 26, 2008 at 14:36 UTC
    #---------------------------------- #HERE IS WHERE I TRY TO USE BUTTONS my $buttonTest = $mw->Button(-text => "test"); # $t->set("3,3", $buttonTest); #wrong $t->windowConfigure("3,3", -window => $buttonTest); #right
    Read the perldoc for TableMatrix for embedded windows and search groups.google.com for "TableMatrix embeded window" for more examples.

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