use Tk; use strict; use warnings; my $mw = new MainWindow(title => "demo"); my $entry = $mw->Scrolled("Text", -scrollbars => 'e', -font => '12')->pack(-side => 'bottom', -fill => 'both', -expand => 1); #$entry->bind("", [sub {print $_[1];}, Ev('A')]); $entry->bind("", [\&color, Ev('A')]); $entry->tagConfigure("blue", -foreground => "blue"); MainLoop; sub color { my $content = $entry->get("1.0", "end"); my $count = 0; my @char = split(//, $content); my @word = split(/ /, $content); foreach my $char (@char) { $count++; } foreach my $word (@word) { if ($word =~ /color/) { my $len = length($word); my $from = "1\.$count"; my $next = $from + $len; $entry->tagAdd("blue", $from, $next); } } }