Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I am building my very first GUI. I have cobbled the bones together but it is an example of poor code, and very redundant. Can I get some pointers on introducing hashes and optimizing this for many "programs"?
use warnings; use strict; use Tk; use Tk::NoteBook; require Tk::Pane; use Tk::LabEntry; my $mw = MainWindow->new; $mw->geometry( "600x500" ); $mw->title("Survival Kit"); ##mainwindow, scroll, frames## my $spane=$mw->Scrolled('Pane')->pack(-expand=>1,-fill=>'both'); my $frame1=$spane->Frame()->pack(-side=>'left',-expand=>1,-fill=>'both +',-padx=>15); my $frame2=$spane->Frame(-padx=>15)->pack(-side=>'right',-expand=>1,-f +ill=>'both'); ##Checkbuttons## my ($pgr1_on,$pgr2_on); my $pgr1_but=$frame1->Checkbutton(-text => "program1 ", -variable => \ +$pgr1_on )->pack; my $pgr2_but=$frame1->Checkbutton(-text => "program2 ", -variable => \ +$pgr2_on )->pack; ##OPEN button## my $open=$mw->Button(-text => "Open\n Tool (s)", -command => \&but)->p +ack( -side=>'bottom', -pady=>20); MainLoop; ####################################### sub but { ## kill top window $spane->destroy($open); ## create notebook #my $sub_spane=$mw->Scrolled('Pane')->pack(-expand=>1,-fill=>'both'); my $book = $mw->NoteBook()->pack(-expand=>1,-fill=>'both'); #my $sub_frame1=$book->Frame()->pack(-side=>'left',-expand=>1,-fill=>' +both'); #my $sub_frame2=$book->Frame()->pack(-side=>'right',-expand=>1,-fill=> +'both'); #####program1####### if ($pgr1_on){ ##create tab,scroll pane,frames my $pgr1_tab=$book->add("Sheet 1", -label => "program1")->pack(-expa +nd=>1,-fill=>'both'); my $sub_spane=$pgr1_tab->Scrolled('Pane')->pack(-expand=>1,-fill=>'b +oth'); my $sub_frame1=$sub_spane->Frame()->pack(-side=>'left',-expand=>1,-f +ill=>'both',-padx=>15); my $sub_frame2=$sub_spane->Frame(-padx=>15)->pack(-side=>'right',-ex +pand=>1,-fill=>'both'); ##now fill frames my @pgr1_default = qw( 1 2 3 4 5 6 7 8 9 10 ); my $pgr1_cnt=0; our $pgr1_parms; foreach my $val qw( PARM1 PARM2 PARM3 PARM4 PARM5 PARM6 PARM7 PARM8 + PARM9 PARM10){ if ($pgr1_cnt < 5){ $sub_frame1->LabEntry(-label => "$val=", -textvariable => "$pgr1_ +default[$pgr1_cnt]" )->pack; } else{ $sub_frame2->LabEntry(-label => "$val=", -textvariable => "$pgr1_ +default[$pgr1_cnt]" )->pack; } $pgr1_parms .= "$val=$pgr1_default[$pgr1_cnt]\n"; $pgr1_cnt++; } } ################### #####program2####### if ($pgr2_on){ ##create tab,scroll pane,frames my $pgr2_tab=$book->add("Sheet 2", -label => "program2")->pack(-expa +nd=>1,-fill=>'both'); my $sub_spane=$pgr2_tab->Scrolled('Pane')->pack(-expand=>1,-fill=>'b +oth'); ##now fill frames my @pgr2_default = ("./","list","./","Y","N"); my $pgr2_cnt=0; our $pgr2_parms; foreach my $val qw( IN_DIR IN_LIST OUT_DIR PARM1 PARM2 ){ $sub_spane->LabEntry(-label => "$val=", -textvariable => "$pgr2_d +efault[$pgr2_cnt]" )->pack; $pgr2_parms .= "$val=$pgr2_default[$pgr2_cnt]\n"; $pgr2_cnt++; } } ############################## ########Exit button########### ##disappears after reset? Implement save? my $leave=$mw->Button(-text => "Save & Exit ",-command => \&save_parms +)->pack; ############################## #######Reset button############# ##needs better implementation? my $return=$mw->Button(-text => "Reset ", -command => \&rtn)->pack( -s +ide=>'bottom', -pady=>20); sub rtn{ system 'perl easydoesit.35basictestBEST2 &'; exit; } ############################# our ($pgr1_parms,$pgr2_parms); sub save_parms { open (SP1,">>pgr1_parms.txt"); print SP1 $pgr1_parms; close SP1; open (SP2,">>pgr2_parms.txt"); print SP2 $pgr2_parms; close SP2; exit; } }
Thanks.

In reply to clunky Tk GUI by honyok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-25 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found