ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have a Scrolled Text widget in my application. I am trying to implement an incremental search of a string on this text widget.
I have come up with the following logic , but it only highlights the first occurence and not the remaining ocuurences. Please help me to make it incremental for the entire text widget.
Following is my code

sub nextText { my $string = ${$searchText->cget(-textvariable)}; $textWindow->FindNext("-forwards","-exact", '-nocase', $string ); }

$searchText is an Entry widget to write the search string and $textWindow is the Tk::Scrolled ('Text') object .
Thanks.

Replies are listed 'Best First'.
Re: Perl tk FindNext in a text widget
by thundergnat (Deacon) on May 29, 2013 at 14:28 UTC

    You aren't supplying enough information for anyone to really help you figure out your problem. The best I can come up with is "You're doing it wrong."

    Try this code snippet to see if it gives you any pointers.

    use strict; use warnings; use Tk; my %w; $w{mw} = MainWindow->new; $w{topfr} = $w{mw}->Frame()->pack( -expand => 1, -fill => 'x' ); $w{textfr} = $w{mw}->Frame()->pack( -expand => 1, -fill => 'both' ); $w{topfr}->Button( -text => 'Previous', -command => sub { nextText('-backwards') } )->pack( -side => 'left' ); $w{searchstring} = $w{topfr}->Entry( -text => 'text', )->pack( -side => 'left' ); $w{topfr}->Button( -text => 'Next', -command => sub { nextText('-forwards') } )->pack( -side => 'left' ); $w{textWindow} = $w{textfr}->Scrolled( 'Text', -scrollbars => 'ose' ) ->pack( -expand => 1, -fill => 'both' ); $w{textWindow}->insert( 'end', q|I have a Scrolled Text widget in my application. I am trying to implement an incremental search of a string on this text widget. I have come up with the following logic , but it only highlights the first occurence and not the remaining ocuurences. Please help me to make it incremental for the entire text widget. Following is my code | ); $w{textWindow}->focus; MainLoop; sub nextText { my $direction = shift; my $string = $w{searchstring}->get; $w{textWindow}->FindNext( $direction, '-exact', '-nocase', $string + ); }

      Hi thundergnat
      Despite insufficient information, you have understood my requirement excellently and exactly this is what I was looking for.
      The only thing is "Previous" button working fine, but the "Next" button is only highlighting the first occurence of the search string and not able to go forward.
      I checked with caller(0) and other print statements. While pressing "Next", it is getting called properly with proper argument i.e "-forwards" but still the problem persists.

      Thanks anyways.

Re: Perl tk FindNext in a text widget
by Anonymous Monk on Jun 30, 2013 at 06:15 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Tk; Main( @ARGV ); exit( 0 ); sub Main { tkgui(0, 'who appears selected ? (with exportselection only)'); tkgui(1, 'who appears selected ? (with exportselection and callbac +ks)'); } sub tkgui { my ( $bind_callbacks , $message ) = @_; my $exportselection = 0; my $mw = MainWindow->new; my $filename = $message; my $f = $mw->Frame->pack( -side => 'top', -fill => 'x' ); my $e = $f->Entry( -textvariable => \$filename, -exportselection => $exportselection, )->pack( -side => 'left', -anchor => 'w', -fill => 'x', -expand => 1, ); my $t = $mw->Scrolled( "Text", -exportselection => $exportselection, )->pack( -side => 'bottom', -fill => 'both', -expand => 1, ); $t->insert( end => join "\n", qw' to hilite highlight selection without w/o focus tagAdd tagname @range tagConfigure foreground and background ', 1 .. 10, ); if( $bind_callbacks ){ $t->bind( '<<Selection>>', \&on_selection ); $t->bind( '<FocusOut>', sub { my ($t) = @_; }, ); } $mw->withdraw; $mw->deiconify; $mw->raise; $mw->focusForce; $mw->update; for( 1 .. 5 ){ $filename = "$message [I HAVE FOCUS]"; $e->selectionRange(0, length($filename) - 8 ); $e->focus ; $e->update; $mw->update; warn 'focusCurrent? ', eval { $mw->focusCurrent } || 'none'; sleep 1; $filename = "$message [I LOST FOCUS]"; $t->selectAll; $t->focus; $t->update; $mw->update; warn 'focusCurrent? ', eval { $mw->focusCurrent } || 'none'; sleep 1; } $mw->withdraw; return $mw; } sub on_selection { my( $t ) = @_; $t->tagDelete('ssel'); if ( my (@range) = $t->tagRanges('sel') ) { $t->tagAdd( ssel => @range ); for my $opt (qw' foreground background ') { $t->tagConfigure( ssel => "-$opt" => $t->cget("select$opt" +) ); } } return; }