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

Re^3: clunky Tk GUI

by zentara (Archbishop)
on Jan 21, 2009 at 16:48 UTC ( #737878=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: clunky Tk GUI
by Anonymous Monk on Jan 22, 2009 at 14:33 UTC

    Hi,

    I think you mean

    my %hash;

    not

    my $hash;
Re^4: clunky Tk GUI
by honyok (Sexton) on Jan 23, 2009 at 22:25 UTC
    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
        OK, I've made changes and trimmed back to bare essentials. I am looping over the %tools hash to create tabs with parameters and defaults. So far, so good. Now, I would like to capture any user changes/inputs and write a text file consisting of: LABEL=ENTRY\n LABEL=ENTRY\n ...
        #!/usr/bin/perl -w use warnings; use strict; use Tk; use Tk::NoteBook; require Tk::Pane; #use Tie::IxHash; use Tk::LabEntry; my $mw = MainWindow->new; $mw->geometry( "600x500"); $mw->title("Survival Kit"); ###program parameter 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 %tools){ ##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',-background=>'slate grey +')->pack(-expand=>1, -fill=>'both'); my $tab_frame1=$tab_spane->Frame(-background=>'blue')->pack(-sid +e=>'left',-expand=>1,-fill=>'both',-padx=>15); my $tab_frame2=$tab_spane->Frame(-background=>'red')->pack(-side +=>'bottom',-anchor=>'s',-fill=>'x'); $tab_cnt++; #create columns my $tab_column1 = $tab_frame1->Frame()->pack(-side=>'left',-exp +and=>1,-fill=>'both'); my $tab_column2 = $tab_frame2->Frame()->pack(-side=>'right',-ex +pand=>1,-fill=>'both'); ##now fill frames my $parm_cnt=1; print "Processing tool: $tab\n"; foreach my $parm ( keys %{$tools{$tab}}) { print " $parm = $tools{$tab}{$parm}\n"; if ($parm_cnt < 6 ){ my $tab_test=$tab_column1->LabEntry(-label => "$parm=") +; $tab_test->Subwidget('entry')->configure(-textvariable => \$t +ools{$tab}{$parm} ); $tab_test->configure(-labelPack=>[-side=>'left']); $tab_test->pack(-anchor=>'e'); } else { my $tab_test=$tab_column2->LabEntry(-label => "$parm=") +; $tab_test->Subwidget('entry')->configure(-textvariable => \$t +ools{$tab}{$parm} ); $tab_test->configure(-labelPack=>[-side=>'left']); $tab_test->pack(-anchor=>'e'); } $parm_cnt++; } #######run button############# my $run=$tab_tab->Button(-text => "\n $tab \n ", -command +=> \&run,-background=>'slate grey')->pack; } MainLoop; ####################### subs ################################# sub run { #print "Label = Entry" to file #execute program }
        Also, how do I keep my hash in order? Tie::IxHash ? Thank you for the help. -honyok

Log In?
Username:
Password:

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

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