john.tm has asked for the wisdom of the Perl Monks concerning the following question:

I have a file which is inserted into a text widget. I have managed to change the foreground colour of the start and end lines that i want changed, but how do i get it to change all the lines in between as well? Here is my code:
sub abcde_guide { $right_frame->delete("1.0", 'end'); open (FH, "$filenameD") or die "\nUnable to open $filename"; while (<FH>) { $right_frame->insert("end", $_); } close (FH); $right_frame->tagConfigure("colf", -foreground => "orange"); $right_frame->tagAdd('colf', '80.5', '81.0 - 3 chars' , '119.5', ' +120.0 - 3 chars ' ); }

Replies are listed 'Best First'.
Re: Perl tk how to change text widget font colour on multiple lines
by zentara (Cardinal) on Oct 04, 2014 at 16:16 UTC
    You need to make your tags before you do your insert, then specify the tag for each line as you insert it.
    # while (<FH>) { $right_frame->insert("end", $_); } # should be something like this while (<FH>) { $right_frame->insert("end", $_, $tag); }
    Here is a complete simple example.
    #!/usr/bin/perl use warnings; use strict; use Tk; open (FH,"<$0") or warn "$!\n"; my @lines = <FH>; close FH; my $mw = MainWindow->new(-title=>'Colored Text'); $mw->geometry(($mw->screenwidth-10) . 'x' . ($mw->screenheight-10) . '+0+20'); $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size => 25 ); my $text = $mw->Scrolled("Text", -scrollbars => 'osw', -background => 'white', -font =>'big', -wrap => 'none', )->pack; $text->tagConfigure('greyline', -background => 'grey95'); my $toggle = -1; for (@lines) { my $greyline; if( $toggle == 1 ){ my $greyline = '' } else { $greyline = 'greyline' } $text->insert('end',"$_",$greyline); $toggle *= -1; } MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh