Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making form with entries using a loop in perl Tk

I have the following code in which I am making a form where I want to enter multiple timesteps, lat, and longitude entriess as shown below:

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 ;

This code is supposed to create a image like this:

Desired form

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to make put the variables frame, timestep, lat, lon and maxwind into arrays so that I can put the code above into a loop? I attempted to do condense the first set of code into a looop below but got an error.

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");   
}

Is there anyway to tweak this code to get the desired result and keep the code more concise?

>Solution :

Scalar variables in Perl have to start with a dollar sign. Declaring a variable is also a good habit:

for (my $i = 0; $i < 10; ++$i) {

There’s also a semicolon missing after the $timestep[$i] assignment. Moreover, my shouldn’t be used for array elements, e.g.

my $lat[$i] = ...  # Wrong!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading