将文本小部件中的所有单词输入到数组中
Get all the word enter in text widget to an array
我是 Tk/Perl 的新手。下面是我使用 tk/perl 创建的简单 GUI 界面。
GUI INTERFACE
下面是创建此 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();
}
我想获取用户在文本中输入的数字(22、23、24、25、26)以在我的子例程中处理,但我无法从 shift -> get() 获取它。我可以通过任何方式获得用户在文本小部件中输入的所有数字吗?感谢您的帮助
Tk::Text
对象上 get()
方法的正确语法在 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
所以在没有参数的情况下使用 get()
是错误的。
下面是一个如何获取文本的例子:
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;
我是 Tk/Perl 的新手。下面是我使用 tk/perl 创建的简单 GUI 界面。
GUI INTERFACE
下面是创建此 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();
}
我想获取用户在文本中输入的数字(22、23、24、25、26)以在我的子例程中处理,但我无法从 shift -> get() 获取它。我可以通过任何方式获得用户在文本小部件中输入的所有数字吗?感谢您的帮助
Tk::Text
对象上 get()
方法的正确语法在 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 isindex2
(the character atindex2
will not be returned). Ifindex2
is omitted then the single character atindex1
is returned. If there are no characters in the specified range (e.g.index1
is past the end of the file orindex2
is less than or equal toindex1
) then an empty string is returned
所以在没有参数的情况下使用 get()
是错误的。
下面是一个如何获取文本的例子:
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;