I did something similar not too long ago. Here's a little demo script showing one way to do it. It has configurable search parameters and highlight colors.

I see zentara has already posted an example. Oh well. Here's another :-) For demonstration purposes, it loads itself into the text widget.

use strict; use warnings; use Tk; my %widget; $widget{main} = MainWindow->new; $widget{text} = $widget{main}->Scrolled( 'Text', -scrollbars => 'se' ) ->pack( -expand => 1, -fill => 'both' ); $widget{hilitemode} = 'regex'; $widget{hilitecolor} = '#ABCDEF'; configure_tag(); $widget{frame} = $widget{main}->Frame()->pack( -side => 'bottom', -anchor => 's' ); $widget{frame}->Label( -text => 'Highlight Character(s) or Regex', ) ->pack( -side => 'top', -pady => 2, -padx => 2, -anchor => 'n' ); $widget{hilight_entry} = $widget{frame}->Entry( -width => 40, -background => 'white', -relief => 'sunken', )->pack( -padx => 3, -pady => 3, -anchor => 'n' ); $widget{subframe} = $widget{frame}->Frame()->pack( -side => 'bottom', -anchor => 's' ); $widget{subframe}->Radiobutton( -variable => \$widget{hilitemode}, -value => 'exact', -text => 'Exact', )->grid( -row => 0, -column => 1 ); $widget{subframe}->Radiobutton( -variable => \$widget{hilitemode}, -value => 'regex', -text => 'Regex', )->grid( -row => 0, -column => 2 ); $widget{subframe}->Button( -text => 'Highlight Color', -command => sub { my $color = $widget{main}->chooseColor( -initialcolor => $widget{hilitecolor}, -title => 'Choose Highlight Color' ); $widget{hilitecolor} = $color if $color; configure_tag(); } )->grid( -row => 0, -column => 3 ); seek( DATA, 0, 0 ); $widget{text}->insert( 'end', $_ ) for <DATA>; $widget{hilight_entry}->insert( 'end', '\$widget\{\w+\}' ); $widget{main}->repeat( 400, \&hilite ); MainLoop; sub hilite { my $highlight_term = $widget{hilight_entry}->get; $widget{text}->tagRemove( 'hilight', '1.0', 'end' ); return unless length $highlight_term; if ( $widget{hilitemode} eq 'exact' ) { $highlight_term = quotemeta($highlight_term); } else { return unless isvalid($highlight_term); } my $length; my $index = '1.0'; while ($index) { $index = $widget{text}->search( '-regexp', -count => \$length, '--', $highlight_term, $index, 'end' ); if ($index) { $widget{text} ->tagAdd( 'hilight', $index, $index . ' +' . $length . ' +c' ); $index = $widget{text}->index( $index . ' +' . $length . ' +c' ); } } } sub configure_tag { $widget{text} ->tagConfigure( 'hilight', -background => $widget{hilitecolor} ) +; } sub isvalid { my $term = shift; return eval { "" =~ m/$term/; 1 } || 0; } __DATA__

In reply to Re: how to open and highlight text by thundergnat
in thread how to open and highlight text by arunmep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.