Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2021-01-23 14:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?