我试图通过循环在Tlx中定义Perl中的按钮序列;每个按钮的文本和操作都在一个哈希表数组中定义。
然而,无论我点击什么按钮,只触发最后一个按钮的动作。
这是我使用的代码。
use strict;
use Tkx;
my @Buttons = (
{ 'descr'=> "Button #0", 'cmd'=> sub { print "TODO: to implement (button #0)\n\n"; } },
{ 'descr'=> "Button #1", 'cmd'=> sub { print "TODO: to implement (button #1)\n\n"; } }
);
my %Widg;
$Widg{"."}= Tkx::widget->new(".");
$Widg{"."}->g_wm_title("List of buttons");
$Widg{"."}->g_wm_minsize(350, 300);
Tkx::font_create("H4", -family => "Helvetica", -size => 8);
$Widg{".fButton"} = $Widg{"."}->new_ttk__frame( -padding => "2 2 5 5");
$Widg{".fButton"}->g_grid(-row => 1, -sticky => "w");
my $id=-1;
foreach my $rh_measure (@Buttons) {
++$id;
$Widg{".fButton.bRun$id"}=$Widg{".fButton"}->new_ttk__button(-text => $rh_measure->{'descr'}, -command => sub { &wrapper($id); });
$Widg{".fButton.bRun$id"}->g_grid( -row => $id, -column => 1, -sticky => "w");
}
Tkx::MainLoop;
sub wrapper {
my $id=shift;
print "Process command for button #$id\n";
&{$Buttons[$id]->{'cmd'}}();
}
答案 0 :(得分:0)
实际上,您只需通过引用调用该命令,并且该参数未被引用。
$Widg{".fButton.bRun$id"}=$Widg{".fButton"}->new_ttk__button(-text => $rh_measure->{'descr'}, -command => [\&wrapper, $id]);