使用 perl Tk 中的循环制作带有条目的表格
Making form with entries using a loop in perl Tk
我有以下代码,我正在制作一个表格,我想在其中输入多个时间步长、纬度和经度条目,如下所示:
my $mw = MainWindow->new;
my $exec_frame2 = $mw->Frame()->pack(-side => "top");
my $title_frame_track = $exec_frame2->Frame()->pack(-side=>"top");
$title_frame_track->Label(-text => '')->pack(-side => "top");
$title_frame_track->Label(-text => 'Storm Coordinates/Track')->pack(-side => "top");
my $frame1 = $exec_frame2->Frame()->pack(-side=>"top");
$frame1->Label(-text => 'Time Step:')->pack(-side=>"left");
my $timestep1 = $frame1->Entry(-width => 15)->pack(-side =>"left");
$frame1->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat1 = $frame1->Entry(-width => 5)->pack(-side =>"left");
$frame1->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon1 = $frame1->Entry(-width => 5)->pack(-side =>"left");
$frame1->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind1 = $frame1->Entry(-width => 5)->pack(-side=>"left");
my $frame2 = $exec_frame2->Frame()->pack(-side=>"top");
$frame2->Label(-text => 'Time Step:')->pack(-side=>"left");
my $timestep2 = $frame2->Entry(-width => 15)->pack(-side =>"left");
$frame2->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat2 = $frame2->Entry(-width => 5)->pack(-side =>"left");
$frame2->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon2 = $frame2->Entry(-width => 5)->pack(-side =>"left");
$frame2->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind2 = $frame2->Entry(-width => 5)->pack(-side=>"left");
my $frame3 = $exec_frame2->Frame()->pack(-side=>"top");
$frame3->Label(-text => 'Time Step:')->pack(-side=>"left");
my $timestep3 = $frame3->Entry(-width => 15)->pack(-side =>"left");
$frame3->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat3 = $frame3->Entry(-width => 5)->pack(-side =>"left");
$frame3->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon3 = $frame3->Entry(-width => 5)->pack(-side =>"left");
$frame3->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind3 = $frame3->Entry(-width => 5)->pack(-side=>"left");
MainLoop ;
此代码应该创建这样的图像:
有没有办法将变量 frame、timestep、lat、lon 和 maxwind 放入数组中,以便我可以将上面的代码放入循环中?我试图将第一组代码压缩到下面的循环中,但出现错误。
for (i=0,i<10;i++)
{
$frame[$i] = $exec_frame2->Frame()->pack(-side=>"top");
$frame[$i]->Label(-text => 'Time Step:')->pack(-side=>"left")
$timestep[$i] = $frame[$i]->Entry(-width => 15)->pack(-side =>"left");
$frame[$i]->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat[$i] = $frame[$i]->Entry(-width => 5)->pack(-side =>"left");
$frame[$i]->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon[$i] = $frame[$i]->Entry(-width => 5)->pack(-side =>"left");
$frame[$i]->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind[$i] = $frame[$i]->Entry(-width => 5)->pack(-side=>"left");
}
是否可以调整此代码以获得所需的结果并使代码更简洁?
Perl 中的标量变量必须以美元符号开头。声明一个变量也是一个好习惯:
for (my $i = 0; $i < 10; ++$i) {
$timestep[$i]
赋值后还少了一个分号。此外,my 不应用于数组元素,例如
my $lat[$i] = ... # Wrong!
我有以下代码,我正在制作一个表格,我想在其中输入多个时间步长、纬度和经度条目,如下所示:
my $mw = MainWindow->new;
my $exec_frame2 = $mw->Frame()->pack(-side => "top");
my $title_frame_track = $exec_frame2->Frame()->pack(-side=>"top");
$title_frame_track->Label(-text => '')->pack(-side => "top");
$title_frame_track->Label(-text => 'Storm Coordinates/Track')->pack(-side => "top");
my $frame1 = $exec_frame2->Frame()->pack(-side=>"top");
$frame1->Label(-text => 'Time Step:')->pack(-side=>"left");
my $timestep1 = $frame1->Entry(-width => 15)->pack(-side =>"left");
$frame1->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat1 = $frame1->Entry(-width => 5)->pack(-side =>"left");
$frame1->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon1 = $frame1->Entry(-width => 5)->pack(-side =>"left");
$frame1->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind1 = $frame1->Entry(-width => 5)->pack(-side=>"left");
my $frame2 = $exec_frame2->Frame()->pack(-side=>"top");
$frame2->Label(-text => 'Time Step:')->pack(-side=>"left");
my $timestep2 = $frame2->Entry(-width => 15)->pack(-side =>"left");
$frame2->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat2 = $frame2->Entry(-width => 5)->pack(-side =>"left");
$frame2->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon2 = $frame2->Entry(-width => 5)->pack(-side =>"left");
$frame2->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind2 = $frame2->Entry(-width => 5)->pack(-side=>"left");
my $frame3 = $exec_frame2->Frame()->pack(-side=>"top");
$frame3->Label(-text => 'Time Step:')->pack(-side=>"left");
my $timestep3 = $frame3->Entry(-width => 15)->pack(-side =>"left");
$frame3->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat3 = $frame3->Entry(-width => 5)->pack(-side =>"left");
$frame3->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon3 = $frame3->Entry(-width => 5)->pack(-side =>"left");
$frame3->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind3 = $frame3->Entry(-width => 5)->pack(-side=>"left");
MainLoop ;
此代码应该创建这样的图像:
有没有办法将变量 frame、timestep、lat、lon 和 maxwind 放入数组中,以便我可以将上面的代码放入循环中?我试图将第一组代码压缩到下面的循环中,但出现错误。
for (i=0,i<10;i++)
{
$frame[$i] = $exec_frame2->Frame()->pack(-side=>"top");
$frame[$i]->Label(-text => 'Time Step:')->pack(-side=>"left")
$timestep[$i] = $frame[$i]->Entry(-width => 15)->pack(-side =>"left");
$frame[$i]->Label(-text => 'Lat:')->pack(-side=>"left");
my $lat[$i] = $frame[$i]->Entry(-width => 5)->pack(-side =>"left");
$frame[$i]->Label(-text => 'Lon:')->pack(-side=>"left");
my $lon[$i] = $frame[$i]->Entry(-width => 5)->pack(-side =>"left");
$frame[$i]->Label(-text => 'Max Wind:')->pack(-side=>"left");
my $maxwind[$i] = $frame[$i]->Entry(-width => 5)->pack(-side=>"left");
}
是否可以调整此代码以获得所需的结果并使代码更简洁?
Perl 中的标量变量必须以美元符号开头。声明一个变量也是一个好习惯:
for (my $i = 0; $i < 10; ++$i) {
$timestep[$i]
赋值后还少了一个分号。此外,my 不应用于数组元素,例如
my $lat[$i] = ... # Wrong!