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

Hello

This is really a low priority question, but been myself a perfectionist in UI design, I was wondering if there is a possibility to set the color Tablematrix uses when a cell/row is selected/in focus/highlighted (I do not know how to express it, sorry). Standard is a a quite old fashioned blue. I tried with $t->tagRow('active',$row); but this is actually not the right way. I can see the selection going blue and then getting the set color (not to mention that I have not implemented the way to reset the original color when another row is selected.

#!/usr/bin/perl use warnings; use strict; 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"; sub colSub{ my $col = shift; return "OddCol" if( $col > 0 && $col%2) ; } sub rowSub{ my $row = shift; return "Oddrow" if( $row > 0 && $row%2) ; } my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 6, -height => 6, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&coloring, -colstretchmode => 'last', -rowtagcommand => \&rowSub, -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se', -relief => 'ridge', -selectmode => 'extended', -selecttype=>'row', ); # Color definitions here: $t->tagConfigure('active', -bg => 'lightyellow', -fg => 'red', -relief + => 'sunken'); $t->pack(-expand => 1, -fill => 'both'); $t->focus; $t->configure (-state=>'disabled'); Tk::MainLoop; sub coloring { print "COLORING SELECTED ROW...\n"; my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; $t->tagRow('active',$row); }

Replies are listed 'Best First'.
Re: Tk Tablematrix set color of selected row
by Takamoto (Scribe) on Apr 06, 2020 at 17:15 UTC

    EDIT: I always get some idea after I post my question...

    This is a better approach. Still not the right one, as it must be possible to set directly that color some way.

    #!/usr/bin/perl use warnings; use strict; 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"; sub colSub{ my $col = shift; return "OddCol" if( $col > 0 && $col%2) ; } sub rowSub{ my $row = shift; return "Oddrow" if( $row > 0 && $row%2) ; } my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 6, -height => 6, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, #-browsecommand => \&coloring, -colstretchmode => 'last', -rowtagcommand => \&rowSub, -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se', -relief => 'ridge', -selectmode => 'extended', -selecttype=>'row', ); $t->bind('<Button-1>', \&coloring); # Color definitions here: $t->tagConfigure('active', -bg => 'lightyellow', -fg => 'black'); $t->tagConfigure('nonactive', -bg => 'white', -fg => 'black'); $t->pack(-expand => 1, -fill => 'both'); $t->focus; $t->configure (-state=>'disabled'); Tk::MainLoop; my $selected = -1; my $prev_selected = -1; $t->bind('<Button-1>', \&coloring); sub coloring { my $t = shift; print "COLORING SELECTED ROW ($prev_selected = $selected)...\n"; my $event = $t->XEvent; $selected = $t->index('@'.$event->x.','.$event->y, 'row'); $t->tagRow('nonactive',$prev_selected) if (defined $prev_selected + && $prev_selected != -1); $t->tagRow('active',$selected) if (defined $selected); $prev_selected = $selected; }
Re: Tk Tablematrix set color of selected row
by kcott (Bishop) on Apr 07, 2020 at 07:03 UTC

    G'day Takamoto,

    I see you've used the tagConfigure() method here (and twice in your follow-up post). I'd say you're on the right track with that; however, you probably want the 'sel' tag. See the Tags section of the Tk::TableMatrix documentation.

    "... set the color ... when a cell/row is selected/in focus/highlighted (I do not know how to express it, sorry)."

    I can understand that it may be difficult to use the exact terms for something that you're unfamiliar with; unfortunately, that becomes a problem when trying to provide an answer.

    I think Tags is what you want. There are other options that you may find useful. Those listed under "STANDARD OPTIONS", such as -highlightbackground and -highlightcolor may serve your purpose (see Tk::options for their documentation). Also, the Tk::TableMatrix option -invertselected might be sufficient for your needs.

    If you read through the documentation, you'll probably find what you want; in addition, by doing this, you'll learn the terminology. As an extra bonus, tags are used by other widgets so you'll also learn principles you can use elsewhere, e.g. Tk::Text; the same applies to other areas such as Indices.

    — Ken

      Thank you, Ken

      Oh, it was as simple as using the 'sel' tag!!! This simple line does exactly what I wanted...

      $t->tagConfigure('sel', -bg => 'lightyellow', -fg => 'black');
Re: Tk Tablematrix set color of selected row
by Anonymous Monk on Apr 06, 2020 at 20:45 UTC