IB2017 has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks
I want my GUI user to be able to select a string in a Text Widget and highlight it in yellow. If a new string is selected, the old selection is removed and the new string is highlighted in yellow. I have the following script which seems to work fine (probably I should have used the <<Selection>>?), except from a small detail: if the user selects an already highlighted string (or part of it), the color (default is light blue) of the selecting mechanism is not shown over the tagged (yellow) string. I would like to see the color of the selection mechanism over my tagged string. Any idea?
use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $text = $mw->Text()->pack(); $text->tagConfigure('selectedTerm', -background =>"yellow", -foregroun +d =>"black"); $text->insert('end',"This is my text and I want to highlight one part +of it.\n"); $text->bind( "<ButtonRelease>", sub { my @tags = $text->tagRanges("selectedTerm"); if ($text->tagRanges('sel')){ if (@tags){ print "Deleting old tags\n"; $text->tagRemove('selectedTerm', $tags[0], $tags[1]); } $text->tagAdd('selectedTerm', 'sel.first', 'sel.last'); my $SelectedTerm = $text->getSelected; print "User selected: $SelectedTerm\n"; } } ); MainLoop();
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Tk Text highlight color
by jcb (Parson) on Sep 28, 2019 at 22:32 UTC |