0

我是 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() 中获取它。有什么办法可以让用户在文本小部件中输入的所有数字?感谢您的帮助

4

1 回答 1

1

get()对象上的方法的正确语法Tk::Text在以下文档中进行了描述Tk::Text

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

从文本中返回一系列字符。返回值将是文本中从索引为的字符开始并在 索引为的字符index1之前结束的所有字符(不会返回字符)。如果省略,则返回单个字符 at。如果指定范围内没有字符(例如超出文件末尾或 小于或等于),则返回空字符串index2index2index2index1index1index2index1

因此,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;
于 2017-07-28T07:41:24.327 回答