Search code examples
perlperltk

Get all the word enter in text widget to an array


I am new to Tk/Perl. Below is the simple GUI interface i create using tk/perl.

GUI INTERFACE

Below is part of the code create this GUI.

$f2_label=$f_frame_top0->Label(-text=>"File",-font=>[-family=>'MS Sans Serif',-size=>9,-weight=>'bold',-underline=>0],-justify=>'left')->pack(-side=>'left',-anchor=>'w',-padx=>1,-pady=>1,);
$f2_entry=$f_frame_top0->Entry(-width=>50,-state=>"normal")->pack(-side=>'left',-anchor=>'w',-padx=>1,-pady=>1,-fill=>'x',-expand=>1);
$f2_file_btn=$f_frame_top0->Button(-text=>"...", -height=>1, -width=>2, -command=> [\&file_search,$tab2,$f2_entry,"TXT"])->pack(-side=>'left',-anchor=>'w',-padx=>1,-pady=>1);

$f3_label=$f_frame_top1->Label(-text=>"Number",-font=>[-family=>'MS Sans Serif',-size=>9,-weight=>'bold',-underline=>0],-justify=>'left')->pack(-side=>'left',-anchor=>'w',-padx=>1,-pady=>1,);
$f3_entry=$f_frame_top1->Text(-width=>10,-height=>10,-wrap=>'word',-state=>"normal")->pack(-side=>'left',-anchor=>'w',-padx=>1,-pady=>1,-fill=>'x',-expand=>1);


$but1_close=$f_frame_bot->Button(-text=>"Close",-command=>sub {destroy $mw}) ->pack(-side=>"right",-anchor=>'e',-padx=>1,-pady=>1);
$but1_exe=$f_frame_bot->Button(-text=>"Run",-command=>[\&fablot_fusesort,$f2_entry,$f3_entry] ) ->pack(-side=>"right",-anchor=>'e',-padx=>1,-pady=>1);

sub fablot_fusesort{
     my $file1 = shift -> get();
     my $number = shift ->get();
}

i want to get the number user enter in Text (the 22,23,24,25,26) to process in my subroutine but i not able to get it from shift -> get(). Any way i can get all the number user enter in the Text Widget? Thanks for helping


Solution

  • The correct syntax for the get() method on a Tk::Text object is described in the documentation for Tk::Text:

    $text->get(index1, ?index2?)

    Return a range of characters from the text. The return value will be all the characters in the text starting with the one whose index is index1 and ending just before the one whose index is index2 (the character at index2 will not be returned). If index2 is omitted then the single character at index1 is returned. If there are no characters in the specified range (e.g. index1 is past the end of the file or index2 is less than or equal to index1) then an empty string is returned

    So using get() without an argument is an error.

    Here is an example of how to get the text:

    use strict;
    use warnings;
    use Tk;
    
    my $mw = MainWindow->new(); 
    
    my $entry = $mw->Text(
        -width=>20, -height => 10, -wrap => 'word', -state => "normal"
    )->pack(
        -padx => 1, -pady => 1, -fill => 'x', -expand => 1
    );
    
    my $button = $mw->Button(
        -text => "Run",
        -command=> sub { fablot_fusesort($entry) }
    )->pack(
        -padx => 1, -pady => 1
    );
    
    sub fablot_fusesort{
        my ( $entry) = @_;
        my $text = $entry->get('1.0','end'); # <-- Gets all the text in the widget
        print "$text";
    }
    MainLoop;