use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $text = $mw->Text()->pack(); $text->tagConfigure('selectedTerm', -background =>"yellow", -foreground =>"black"); $text->insert('end',"This is my text and I want to highlight one part of it.\n"); $text->bind( "", 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();