我使用的是由kmx实现的Perl模块,我喜欢它,因为它很容易使用,而且看上去还不错。
我需要创建一个框架框与多个按钮从一个列表(假设40-50)。我可以很容易地在遍历数组的for循环中创建这个函数(double Array,包含"Name“和每一行的值)
my @ChildsSetOfButtons=();
foreach $array (@ITEMNAME)
{
$tempButton = IUP::Button->new( TITLE=>$array->[2],
SIZE=>"50x20", FONTSTYLE=>'bold',FONTSIZE=>"10");
$tempButton->ACTION( sub {selOrder($array->[3]); });
}
push(@ChildsSetOfButtons,$tempButton);
my $tableOfButton = IUP::Frame->new( TITLE=>"Items",child=>
IUP::GridBox->new( child=> [@ChildsSetOfButtons], NUMDIV=>4,
ORIENTATION=>"HORIZONTAL"), MARGIN=>"5x5",
EXPANDCHILDREN =>"YES",GAPLIN=>10, GAPCOL=>10);在一个漂亮的网格中,按钮在GUI中显示得很漂亮。现在我的问题是,如何从每个Button操作发送单独的唯一值?我知道在创建按钮时,这些值是静态链接的。
但是,这种类似的实现,我可以让它在PerlTK中工作,因为我从这个IUP开始,我不想回到TKperl,开始从sratch开始编写我的整个GUI。`
foreach my $array (@ITEMNAME)
{
$fr2->Button(-text=>$array->[2],
-padx => 6,
-font => ['arial', '10', 'normal'],
-foreground => 'blue',
-command => [\&writeUARTDirect,$array->[3]],
)->pack(-side=>'bottom');
}‘我怎样才能让它在perl框架中工作?有人能告诉我一个窍门吗?)
发布于 2021-01-30 08:54:14
你的例子有一些问题。我加了一点,使它可以运行:
use IUP ':all';
use strict;
use warnings;
use v5.30;
my @ITEMNAME = ([0,0,foo=>'btn_foo'],
[0,0,bar=>'btn_bar']
);
my @ChildsSetOfButtons=();
foreach my $array (@ITEMNAME)
{
my $tempButton = IUP::Button->new(
TITLE=>$array->[2],
SIZE=>"50x20",
FONTSTYLE=>'bold',
FONTSIZE=>"10");
# alternative: copied value
# my $value = $array->[3];
# $tempButton->ACTION( sub {selOrder($value); });
$tempButton->ACTION( sub {selOrder($array->[3]); });
push(@ChildsSetOfButtons,$tempButton);
}
my $tableOfButton = IUP::Frame->new(
TITLE=>"Items",
child=>
IUP::GridBox->new( child=> [@ChildsSetOfButtons],
NUMDIV=>4,
ORIENTATION=>"HORIZONTAL"),
MARGIN=>"5x5",
EXPANDCHILDREN =>"YES",
GAPLIN=>10,
GAPCOL=>10);
my $dlg = IUP::Dialog->new( TITLE=>"Test Dialog",
MARGIN=>"10x10",
child=> $tableOfButton,);
# Do you want changes like these being reflected in the callback?
$ITEMNAME[1]->[3] = 'another_value';
$ITEMNAME[0] = [];
$dlg->Show;
IUP->MainLoop;
sub selOrder{
my $val = shift;
say "you pressed $val";
}我添加了strict并使您的变量词法化。您在循环中创建按钮,但push语句位于循环之外。因此,您的按钮没有添加到@ChildsSetOfButtons中。
您的回调子引用$array->[3] (别名为@ITEMNAME ),如果@ITEMNAME中的值被更改,这可能会导致意外的副作用。您可以通过将值复制到循环中的词法变量并在回调中使用它来避免这种情况。这样,您就可以得到一个闭包,其值与@ITEMNAME解耦。
https://stackoverflow.com/questions/65963507
复制相似问题