in reply to TK Gui Help

First, ditto on the comments above. You make $f1 a global, but you create the scrolled listbox in another sub using my $f1. The $f1 won't be seen in genchart that way.

You also don't need "use Tk::BrowseEntry" since you are using scrolled listboxes.

Your code is pretty messed up to try and fix, but I will show you how to get the selections out of a set of scrolled listboxes. Notice in the following example, the indice count starts at 0, and to get the text of the indice you need to run $lb->get on it.

#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->geometry(q/+100+100/); $mw->title("Test Script"); #need exportselection=>0 for using multiple selection boxes my $lb0 = $mw->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack; $lb0->insert( 'end', qw/foo bar baz/ ); my $lb1 = $mw->Listbox(-bg => 'lightseagreen', -selectmode => 'extended', -exportselection=> 0 )->pack; $lb1->insert( 'end', 1..20 ); print "$lb0 : $lb1\n"; $mw->Button( -text => "Selections", -command => sub{ print "1: ", join(" ", $lb0->curselection), "\n", "2: ", join(" ", $lb1->curselection), "\n", "\n"; my @selections0 = $lb0->curselection; my @selections1 = $lb1->curselection; foreach my $select (@selections0){ print $lb0->get($select) +,"\n"}; foreach my $select (@selections1){ print $lb1->get($select) +,"\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

Replies are listed 'Best First'.
Re^2: TK Gui Help
by GuiPerl (Acolyte) on Jun 26, 2014 at 17:42 UTC
    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.
      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.

      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