发新话题
打印

Perl/Tk FAQ - 10.16. 如何获取输入框(Entry)中的内容

Perl/Tk FAQ - 10.16. 如何获取输入框(Entry)中的内容

  原文:

10.16. How do I get the entry in an Entry?

You want to call get on the return value of the widget itself. Here is how it may be used in a simplified version of example 1.1 from the Tk::UserGuide where a Button is set up to call a sub where the call to get lies: #!/usr/bin/perl -w use strict; use Tk; my $main = MainWindow -> new(); my $entry = $main -> Entry(); $entry -> pack; $main->Button(-text => 'Print', -command => sub{do_print($entry)} )->pack; MainLoop; sub do_print { my ($widget) = @_; my $entered = $widget -> get(); print "The string "$entered" was entered.\n"; }



译文:

10.16. 如何获取输入框(Entry)中的内容?



简单的说,就是对此输入框调用“get”方法。下面的例子是从UserGuide中的example1.1简化来的,其中设置了一个按钮——关联了一个调用get方法的子程序:

#!/usr/bin/perl -w

use strict;

use Tk;



my $main = MainWindow -> new();

my $entry = $main -> Entry();

$entry -> pack;

$main->Button(-text => 'Print',

-command => sub{do_print($entry)}

)->pack;

MainLoop;



sub do_print {

my ($widget) = @_;

my $entered = $widget -> get();

print "The string "$entered" was entered.\n";

}

TOP

发新话题