in reply to Re: TK Gui Help
in thread TK Gui Help

Thanks for all the input. I have now gone about creating another version implementing everyone's suggestions, however, there remains the issue of capturing values from additional widgets added dynamically. I cannot foresee how many widgets (i.e. listbox or entry ) will be added by the user. And so, I would really appreciate some additional pointers regarding how to implement this kind of coding. I cannot find any suggestions based on Learning Perl/Tk nor Mastering Perl/Tk. Here is the new, yet, still defective code. I have implemented the grid method to dynamically generate the widgets. As you can see, I can retrieve values only for one row of list and entry boxes based on static assignment but nothing beyond this. Moreover, the entry boxes overlap the listbox widgets.
use strict; use warnings; use Tk; my $lb=1; my $textboxrow = 2; my $textboxrowMid = 2; my $fieldbuttonrow = 3; my $textbox = 0; my $textbox2 = 1; my $textboxMid = 0; my $textboxMid2 = 1; my ($be,$be1, @be,@me,$label,$label2, $ent,$ent2,$entry_1,$entry_2); my $mw = tkinit; $mw -> geometry("400x300"); my $button = $mw->Button(-text => 'Add Field', -command => \&addtext +box); my $button2 = $mw->Button(-text => 'Print texts',-command => \&printte +xts); addtextbox(); MainLoop(); sub addtextbox{ my @divisions = qw (AF CSPL CSO DGG DG FR FRAT OCX OC); $label=$mw->Label(-text=>"Division Unit:")->pack(); $ent = $mw -> Entry(-textvariable => \$entry_1,) -> pack(); $be = $mw->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack; $be->insert( 'end', @divisions ); $label2=$mw->Label(-text=>"Division Unit:")->pack(); $ent2 = $mw -> Entry(-textvariable => \$entry_2,) -> pack(); $be1 = $mw->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack; $be1->insert( 'end', @divisions ); $label ->grid(-row=>$textbox++, -column=>1); $ent ->grid(-row=>$textbox2++, -column=>1); $be ->grid(-row=>$textboxrow++, -column=>1); $label2 ->grid(-row=>$textboxMid++, -column=>2); $ent2 ->grid(-row=>$textboxMid2++, -column=>2); $be1 ->grid(-row=>$textboxrowMid++, -column=>2); $button ->grid(-row=>$fieldbuttonrow++,-column=>1,-columnspan=>2); $button2->grid(-row=>$fieldbuttonrow ,-column=>1,-columnspan=>2); } sub printtexts { my @entry_1 = $ent->get(); my @entry_2 = $ent2->get(); my @values = $be->curselection(); my @values2 = $be1->curselection(); foreach (@entry_1) { print $_,"\n" } foreach my $v (@values) { print $be->get($v),"\n"; } foreach (@entry_2) { print $_,"\n" } foreach my $vv (@values2) { print $be1->get($vv),"\n"; } }
Many thanks again.

Replies are listed 'Best First'.
Re^3: TK Gui Help
by zentara (Cardinal) on Jun 27, 2014 at 10:26 UTC
    however, there remains the issue of capturing values from additional widgets added dynamically.

    When in doubt, just start stuffing selections into a global hash, then the selections will always be available. You get your data out by looping thru the hash keys. Every new widget gets a new hash entry, so you can add to your heart's content, keeping everything in a hash.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      An example would be appreciated of growing a dynamic hash for every new widget. The hash will need to store the same data set, yet, is treated uniquely by the GUI even if the selected item(s) is/are the same value. I was thinking of something along these lines:
      my $count = 0; my %division{$_} = ( 1 => 'AF', 2 => 'CSS', ); my $addbutton = $f->Button(-text => "Add Chart Blocks", -command => [\&addwidget,$division{$count++}], )->pack(-anchor => +'s');
      This way the hash would come in as division1, division2, etc. The counter would increment every time the button is pressed and thus creating a unique hash name based on the event. Does this make some sense? Thanks for all the patience and help thus far.
        You sort of have the right idea, but there is one thing to watch out for: do not try to use the widget as a hashkey, as it will result in an error. You are doing it right by assigning a counter. Here is an example based on my code example above. P.S. To make your code look better, put your listboxes into Scrolled Panes. This example needs some work to make it look right, but it demonstrates the hash idea quite nicely.
        #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry(q/+100+100/); $mw->title("Test Script"); #need exportselection=>0 for using multiple selection boxes my %lbhash; # you want to use the listbox to hold your # data, so store the widget's actual object # with the name you want to give it my @choices = ('afg', 'dfg', 'erty', 'etc'); foreach my $choice(@choices){ my $pane = $mw->Scrolled('Pane')->pack(-side => 'left'); my $label = $pane->Label(-text=> $choice, -bg=>'lightseagreen') ->pack(-side=>'top'); $lbhash{ $choice }{'widget'} = $pane->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack(-side=>'bottom'); my @random_data; for(1..20){push @random_data ,int rand(100) } $lbhash{ $choice }{'widget'} ->insert( 'end', @random_data ); } $mw->Button( -text => "Selections", -command => sub{ foreach my $key( keys %lbhash ){ my @selects = $lbhash{ $key }{'widget'}->curselection; print "$key: "; foreach my $select (@selects){ print $lbhash{ $key }{'widget'}->get($select),' '; } print "\n\n"; } } )->pack( -side => 'left' ); $mw->Button( -text => "Done", -command => \&exit )->pack; MainLoop;

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re^3: TK Gui Help
by Anonymous Monk on Jun 26, 2014 at 23:56 UTC

    Why are you calling addtextbox() without arguments?

    Do it like this, write code this way, pass arguments all over, keep track of the life cycle of your variables, its coping with scoping, only use variables you declare pass, never any others

    sub GoTkGui { my $mw = tkinit; my @stackOfEntries; ## or %division or %meaningfulName SetUpTheCheeze( $mw, \@stackOfEntries ); } sub SetUpTheCheeze { my( $mw, $entriesRef ) = @_; ... $mw->Button(-text => 'Add Field', -command => [ \&addtextbox, $mw, + $entriesRef ], ); ... } sub addtextbox { my( $mw, $entriesRef ) = @_; ... ## you decide what you need want ## push @$entriesRef, $ent; ## push @$entriesRef, $entry_1; }

    Re: Tk: Creating label in hash Re: TK Submenus (Tk::Menu , global variables/ spirit of strict), Re: widget box problem in tk