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

I have found that I can set column widths using $wg->colWidth
Also I want to set characteristics (for example colour, state) of individual cells of a tablematrix.
So far, the only way I have found of setting the a cell’s colour and state is to:
1. Define a tag with the characteristics I want;
2. Use $wg->tagCell to add one of the defined tags to the cell.
This works as the code below shows. Is this the preferred way of setting cell characteristics?
#!/usr/bin/perl use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my (%wg, %hashv); print "Filling Array...\n"; my ($rows,$cols) = (10, 10); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $hashv{t1}{"$row,$col"} = 2*$row + 3*$col; $hashv{t2}{"$row,$col"} = 2*$row + 3*$col + 100; } } print "Creating Table t1\n"; $wg{t1} = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => \%{$hashv{t1}}, -coltagcommand => \&colSub1, -browsecommand => \&tbmx_browse_cmd_t1, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $wg{t1}->grid(-row => 0, -column => 0); # set the column widths $wg{t1}->colWidth(0 => 2, 1 => 4, 2 => 6, 4 => 8); # defining tags $wg{t1}->tagConfigure('dis', -state => 'disabled', -bg => 'yellow'); $wg{t1}->tagConfigure('red', -bg => 'red'); $wg{t1}->tagCell('dis',"1,1"); $wg{t1}->tagCell('dis',"1,2"); $wg{t1}->tagCell('dis',"1,3"); $wg{t1}->tagCell('red',"3,1"); $wg{t1}->tagCell('red',"3,2"); $wg{t1}->tagCell('red',"3,3"); #===================================================== # # tbmx_browse_cmd_t1 # # browse command for table matrix t1 # #===================================================== sub tbmx_browse_cmd_t1() { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; print "[tbmx_browse_cmd_t1] index actual <$actual_index> previous <$pr +evious_index> row <$row> col <$col>\n"; } Tk::MainLoop;

Replies are listed 'Best First'.
Re: TableMatrix - setting cells characteristics - eg colour & state
by zentara (Cardinal) on Jun 09, 2011 at 15:03 UTC
    Is tags the preferred way of setting cell characteristics?

    For the TableMatrix I think it is the only way. The HList widget uses Tk::ItemStyle, which I've only seen used on the HList. There is also the optionAdd method. A simple example:

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::HList; use Tk::ItemStyle; my $mw = MainWindow->new(); my $green = $mw->ItemStyle( 'text', -stylename => 'green', -bg => 'green', -fg => 'black', ); $mw->optionAdd("*background", 'red'); $mw->optionAdd("*foreground", 'blue'); my $hlist; $hlist = $mw->HList( -itemtype => 'text', -selectmode => 'single', )->pack(-fill=>'both',-expand=>1); $hlist->columnWidth(0, -char, 90); for (1..1000) { $hlist->add($_, -text=>$_); } for (my $i = 1 ; $i < 1000 ; $i += 2){ $hlist->entryconfigure( $i, -style => 'green' ); } MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks for that. It is not a problem but it just looked and felt a bit odd!