#!/usr/bin/perl -w use strict; use Tk; # # Create the main window and stick the widgets in it... # my $mw = MainWindow->new(); $mw->geometry("600x300"); my $one_F = $mw->Frame( )->pack(-side => 'top', -fill => 'x', -expand => 1, -padx => 3, -pady => 3, ); my $two_F = $mw->Frame( )->pack(-side => 'top', -fill => 'x', -expand => 1, -padx => 3, -pady => 3, ); $mw->Label(-text => "Selected Text ->", )->pack(-side => 'left', -in => $one_F, ); my $selected = ""; $mw->Entry(-textvariable => \$selected, )->pack(-side => 'left', -in => $one_F, ); my $tb = $mw->Scrolled("Text", -wrap => 'none', )->pack(-side => 'left', -in => $two_F, -expand => 1, -fill => 'both', ); # # Define the 'red' tag # $tb->tagConfigure('hl', -foreground => "red", ); $tb->tagBind('hl', "", sub{ ( $selected ) = &Select_Text( $tb, 'hl', 'line' ); }, ); # # Define the underline tag # $tb->tagConfigure('ul', -underline => 1, ); $tb->tagBind('ul', "", sub{ ( $selected ) = &Select_Text( $tb, 'ul', 'word' ); }, ); # # Add a bunch of repetitive lines to the text box, assigning some to # tags... # for ( 1 ..100 ) { $tb->insert("$_.0", "This is line # $_\n" ); if ( $_ % 5 == 0 ) { $tb->tagAdd('hl', "$_.0 linestart", "$_.0 lineend" ); } if ( $_ % 3 == 0 ) { $tb->tagAdd('ul', "$_.8", "$_.12" ); } } MainLoop(); exit 0; # # Get the text that is assigned to the tag. # sub Select_Text { my( $tb, $tag, $type ) = @_; my $selected = ""; if ( $type eq "line" ) { chomp( $selected = $tb->get( $tb->tagNextrange("$tag", "current -1 lines" ) ) ); } elsif ( $type eq "word" ) { chomp( $selected = $tb->get( $tb->tagNextrange("$tag", "current wordstart" ) ) ); } return( $selected ); }