Hi perl monks, I am having an issue with retrieving values from a dynamically generated group of combo boxes. I am using an array to hold all of these combo boxes. I can only seem to access the most recent combo box and I get an error whenever I try to access the contents of an older combo box (so if I have two combo boxes, I get an error when I try to access the first one).

Here is the error that I get: Tk::Error: bad listbox index "": must be active, anchor, end, @x,y, or a number at /usr/intel/pkgs/perl/5.8.5/lib/site_perl/5.8.5/x86_64-linux/Tk.pm line 247. Tk callback for .browseentry.toplevel.frame.listbox Tk callback for .button1 Tk::__ANON__ at /usr/intel/pkgs/perl/5.8.5/lib/site_perl/5.8.5/x86_64-linux/Tk.pm line 247 Tk::Button::butUp at /usr/intel/pkgs/perl/5.8.5/lib/site_perl/5.8.5/x86_64-linux/Tk/Button.pm line 111 <ButtonRelease-1> (command bound to event)

Here is my code:

use strict; use warnings; use Tk; my $fieldcounter = 0; my $textboxrow = 1; my $fieldbuttonrow = 2; my @be; my @listb; my $mw = tkinit; $mw -> geometry("400x200"); $be[$fieldcounter] = $mw->BrowseEntry(-label=>"Field") ; $listb[$fieldcounter] = $be[$fieldcounter]->Subwidget('slistbox')->Sub +widget('scrolled'); my @content; tie @content,'Tk::Listbox', $listb[$fieldcounter]; @content = qw/dog cat mouse/; my $button = $mw->Button(-text => 'Add Field',-command => \&addtextbox +,); my $button2 = $mw->Button(-text => 'Print texts',-command => \&printte +xts,); $be[$fieldcounter] -> grid(-row=>$textboxrow,-column=>2); $button ->grid(-row=>$fieldbuttonrow,-column=>2,-columnspan=>2); $button2 ->grid(-row=>$fieldbuttonrow+1,-column=>2,-columnspan=>2); MainLoop(); sub addtextbox{ $fieldcounter++; $fieldbuttonrow++; $textboxrow++; $be[$fieldcounter] = $mw->BrowseEntry(-label=>"Field"); $listb[$fieldcounter] = $be[$fieldcounter]->Subwidget('slistbox')->Sub +widget('scrolled'); tie @content,'Tk::Listbox', $listb[$fieldcounter]; @content = qw/dog cat mouse/; $be[$fieldcounter] -> grid(-row=>$textboxrow,-column=>2); $button->grid(-row=>$fieldbuttonrow,-column=>2,-columnspan=>2); $button2->grid(-row=>$fieldbuttonrow+1,-column=>2,-columnspan=>1); } sub printtexts{ my $field_id; my $field; my $fields; my @fieldslist; for my $listb_index(0 .. $fieldcounter){ print "listb_index is $listb_index\n"; $field_id = $listb[$listb_index] -> curselection(); $field = $listb[$listb_index] -> get($field_id); push @fieldslist, $field; } $fields = join (':', @fieldslist); print "fields is: $fields\n"; }

In reply to perltk: error getting values from dynamically generated combo boxes by ruhroh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.