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

Hi,

Using the TableMatrix/Spreadsheet & need to keep bg color of selected row even when widget lost focus and another window /widget got focus.

Both method are ok:

Please advise.

Replies are listed 'Best First'.
Re: TK spread sheet color row
by kcott (Archbishop) on Jul 09, 2017 at 08:49 UTC

    G'day aviw,

    You can get and set options (including -background, -selectbackground, and lots of other -*background variations) using the cget() and configure() methods. See Tk::options.

    You can bind actions to events (such as <FocusIn>, <FocusOut>, <Enter>, <Leave>, and so on) which could control colours and other options. See Tk::bind.

    [ Note: In your last Tk-related post, "tk sash slide render", you did much the same as you've done here: including no code and no links to the modules you're using. You were advised about this in that post. If you want better answers, I suggest you reread that advice then provide a better question.]

    — Ken

      Hi,

      Thank you both.

      I would prefer to use common TK way (-selcmd => sub{}) I do get click event yet it returns my ($NumRows,$Numcols,$selection,$noCells) = @_. based on: TableMatrix

      When using -rowtagcommand I do get a call with correct row number (great!) yet I fail to find a way to color the row :/

      Is the only solution is via TK:bind for each cell?

        yet I fail to find a way to color the row

        Hi, you can try

        $t->tagRow('active', $row);
        and here is a full working example. Press the Update button to see it work.
        #!/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"; ## 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) ; } 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, -colstretchmode => 'last', -rowtagcommand => \&rowSub, -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', -fg => 'red', -relief => 's +unken'); $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 { $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 }

        I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: TK spread sheet color row
by Corion (Patriarch) on Jul 09, 2017 at 08:03 UTC

    Which method have you already tried and how did it fail for you to work?

    Without knowing too much about Tk, I would first try the second approach.

      Tried the 2nd and found this good example:link

      Yet failed to understand how to effect specific row with tagConfigure dynamically upon rowtagcommand call.