Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: clunky Tk GUI

by honyok (Sexton)
on Jan 21, 2009 at 16:21 UTC ( #737873=note: print w/replies, xml ) Need Help??


in reply to Re: clunky Tk GUI
in thread clunky Tk GUI

Thank you. I adapted this from your previous reply but, as a PERL dabbler, I am a bit perplexed by your hash structures. Can you add more explanation to your earlier code?

Replies are listed 'Best First'.
Re^3: clunky Tk GUI
by zentara (Archbishop) on Jan 21, 2009 at 16:48 UTC
    Well it's all about hashes, and it's a big topic. I will reccomend you to the tutorials at Data Type: Hash. Now, I will try to give a brief explanation of how it all works.

    Instead of storing individual widgets in separate scalars, you can store those scalars in a orderly hash structure, which will allow you to access them by a key. So for instance, say you had 2 pages, and 3 widgets on each page. You could be "clunky" and call them $page1_widget1, $page1_widget2, $page1_widget3, and $page2_widget1, $page2_widget2, $page2_widget3.

    You can see how clunky that is. An alternative, would be to store them all in a hash, like

    my %hash; # NOT my $hash, unless you want to set it up reference sty +le # then create in loops $hash{$page}{$widget} #like foreach my $page(1..2){ #add the page $hash{$page}{'page'} = $nb->add(......) #fill each page with widgets foreach my $widg( 1..3){ $hash{$page}{$widj} = $hash{$page}{'page'}->Button(.....opt +ions.) } }
    Now, all you need to do to access any widget, is the $page and $widget number. For instance, the 2ond widget on page2, would be $hash{2}{2}. ( or $hash{$page}{$widj} )

    This is a very simple example of course, and real world apps each need the best hash setup. It sort of is an art that is learned by spending a few afternoons setting up hashes and experimenting. There are 2 great benefits to storing widgets in a hash.

    1. It makes accessing and looping thru them easy. You can loop thru all the widgets on page2 for instance.

    2. And this comes later.... if you want to put your script into a package or module, the hash is already setup to be blessed. Instead of $hash{$page}{$widj} you would have $self->{$page}->{$widj}.

    A whole book could be written on this, so I'm stopping now. Just play around with simple apps, and store things in hashes...it won't take long for you to figure it out.


    I'm not really a human, but I play one on earth Remember How Lucky You Are

      Hi,

      I think you mean

      my %hash;

      not

      my $hash;
      Thank your for your patience and help. I have improved the code a bit, but have hit a snag. I am using a hash reference to call a Checkbutton variable. The reference on line 55 , $flags{$tab} is the variable $pgr1_flag. Evidently this is ill-advised. Advice?
      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_flag,$pgr2_flag); my %flags = qw(program1 $pgr1_flag program2 $pgr2_flag); my $cb_cnt=1; for my $flag (keys %flags){ if ($cb_cnt < 6 ){ my $flag_but=$frame1->Checkbutton(-text => "$flag ", -variable + => \$flags{$flag} )->pack; } else { my $flag_but=$frame2->Checkbutton(-text => "$flag ", -va +riable => \$flags{$flag} )->pack; } $cb_cnt++; } ##OPEN button## my $open=$mw->Button(-text => "Open\n Tool (s)", -command => \&but)->p +ack( -side=>'bottom', -pady=>20); MainLoop; ####################### subs ################################# sub but { ## kill top window $spane->destroy($open); ###default hash my %tools = ( 'program1' => { 'PARM1' => 1, 'PARM2' => 'n', 'PARM3' => + 'y', 'PARM4' => 'n', 'PARM5' => 'y', 'PARM6' => 'n', 'PARM7' => 'y', + 'PARM8' => 'n', 'PARM9' => 'y', 'PARM10' => 'n' }, 'program2' => { ' +PARM1' => 1, 'PARM2' => 2, 'PARM3' => 3, 'PARM4' => 4, 'PARM5' => 5, +'PARM6' => 6, 'PARM7' => 7, 'PARM8' => 8, 'PARM9' => 9, 'PARM10' => 1 +0 }); ## create notebook my $book = $mw->NoteBook()->pack(-expand=>1,-fill=>'both'); #####program1####### my $tab_cnt=1; for my $tab (keys %flags){ if ($flags{$tab}){ ##create tab,scroll pane,frames my $tab_tab=$book->add("Sheet $tab_cnt", -label => "$tab")->pack +(-expand=>1,-fill=>'both'); my $tab_spane=$tab_tab->Scrolled('Pane')->pack(-expand=>1,-fill= +>'both'); my $tab_frame1=$tab_spane->Frame()->pack(-side=>'left',-expand=> +0,-fill=>'both',-padx=>15); my $tab_frame2=$tab_spane->Frame(-padx=>15)->pack(-side=>'right' +,-expand=>0,-fill=>'both'); $tab_cnt++; ##now fill frames my $parm_cnt=0; print "Processing tool: $tab\n"; foreach my $parm ( keys %{$tools{$tab}}) { print " $parm = $tools{$tab}{$parm}\n"; if ($parm_cnt < 5){ $tab_frame1->LabEntry(-label => "$parm=", -textvariab +le => "$tools{$tab}{$parm}" )->pack; } else { $tab_frame2->LabEntry(-label => "$parm=", -textvariab +le => "$tools{$tab}{$parm}" )->pack; } my $tab_parms .= "$parm=$tools{$tab}{$parm}\n"; $parm_cnt++; } } } ###################SAVE parameters? ## button - disappears on reset? #my $save1=$sub_spane->Button(-text => "Save Parameters ")->pack; ########Exit button########### ## 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 doOver.1 &'; 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; }
      BJ
        I don't understand what you are doing.There is no $flags{$tab} in your example. Keep trying. If you want more help, explain what is going wrong from your perspective.

        I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://737873]
help
Chatterbox?
and the web crawler heard nothing...

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