将文本小部件中的所有单词输入到数组中



我是Tk/Perl的新手。下面是我使用 tk/perl 创建的简单 GUI 界面。

图形用户界面

下面是创建此 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?)

从文本中返回一系列字符。返回值将为 文本中以索引为index1并在索引index2之前结束( 字符index2将不返回(。如果省略index2则 返回index1处的单个字符。如果没有字符 在指定范围内(例如index1已超过文件末尾或index2小于或等于index1(,则空字符串为 返回

因此,使用不带参数的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;

最新更新