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. | [reply] [d/l] |
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.
| [reply] |
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. | [reply] [d/l] |
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 | [reply] [d/l] |