I think you have to add your event (button check for example, lost focus...), to check the text, I didn't find this binding on Text widget. Maybe with "Grab" to get all input.
The following code will tell you if the text has been found (in console) and will scroll to the text searched.
You could also highlight this text (with Tags) but I don't have enough time and it's out of scope of my application lol (I don't want to create an editor XD).
I hope it will help you...
Peace
use strict;
use warnings;
use Tk;
use feature qw(say);
my $lblText = "Search:";
my $searchText = "";
my $txtContent = "Perl/Tk is probably the most widely known GUI for Pe
+rl. It is a great interface used by thousands of people. I am writing
+ this to teach those with no previous knowledge of any programming la
+nguage or GUI interface. You should know some Perl (you don't need to
+ be an expert, but you need to know some basic/intermediate Perl) bef
+ore you read this also. Perl/Tk is a module, so it should be fairly e
+asy to get. If you don't know how to install modules";
$txtContent .= reverse $txtContent;
$txtContent =~ s/ /\n/g;
# ponctuation not allowed
my $mw = new MainWindow(-title => "Search demo");
my $lbl = $mw -> Label(-text => $lblText)->pack();
my $search = $mw -> Entry(-textvariable => \$searchText,
-validate => "key",
-validatecommand => sub {$_[1] !~ /[;,.:!?]/},
+
-invalidcommand => sub {$mw->bell})->pack();
my $textarea = $mw -> Frame() ->pack();
my $txt = $textarea -> Text(-width => 40,
-height => 10)->pack();
$txt->insert('end', $txtContent);
my $btnCheck = $mw ->Button(-text => "Check",
-command => sub {&check_text();})->pack();
+
MainLoop;
sub check_text()
{
return if (length $searchText == 0);
say "Searching: ", $searchText;
my $result = $txt->search(-nocase => $searchText, 'end');
if (defined($result))
{
say "Found at pos: $result";
$txt->see($result);
}
else
{
say "Not found!";
}
}
__END__
|