In the interests of TIMTOWTDI, here is another example. This one will display the line or the word associated with the click on tag, depending upon which how the tags were set. In your case, since you've mentioned that you are setting the tags to various words, I would use the word logic, but I included the code for selecting entire lines as well as it is odd enough to be worth mentioning.

Without further ado, here is the code:

#!/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', "<Button-1>", sub{ ( $selected ) = &Select_Text( $tb, 'hl', 'line' ) +; }, ); # # Define the underline tag # $tb->tagConfigure('ul', -underline => 1, ); $tb->tagBind('ul', "<Button-1>", 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 wordstar +t" ) ) ); } return( $selected ); }

This was run on Win NT, using ActiveState Perl 5.6.1. This code will handle you having multiple words assigned to the same tag - it probably won't handle you assigning half a word to one tag and the other half to another tag.

Hopefully this helps. Even if you don't use it, I still enjoyed the challenge...

Pat


In reply to Re: Using Perl/Tk text tags by BlueBlazerRegular
in thread Using Perl/Tk text tags by Anonymous Monk

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.