Dear Monks,

The following script, which is in a routine invoked as a toplevel, is working fine except when I make a second attempt to run it. Essentially I have a script that reads and parses a file, creates an array which is dislayed in a TK::MListbox. The user selects specific elements from one listbox to drop in the second listbox (ie my @elements = $lb2->curselection; in sub select_tag and sub unselect_tag). This works fine the first time, however it doesn't the second time around. Essentially my array of elements to select is empty the second time I open my toplevel. But I am lexically scoping all my variables! The only way I can get this to work is by closing the application and starting afresh. Can anybody see what I am doing wrong? I even tried to withdraw both mlistboxes (ie $lb and lb2) but to no avail! Thank you guys.

my ($tl21); if (! Exists($tl21)){ $tl21 = $mw->Toplevel(-title=>"Report"); $tl21->Label->pack(-side => 'top'); my $f1= $tl21->Frame(-borderwidth =>2, -relief=> 'groove')->pack(- +side => 'top', -fill => 'x'); my $f2= $tl21->Frame(-borderwidth =>2, -relief=> 'groove')->pack(- +side => 'top', -fill => 'x'); my $f3= $tl21->Frame(-borderwidth =>2, -relief=> 'groove')->pack(- +side => 'top', -fill => 'x'); $lb = $f1->Scrolled("Listbox", -scrollbars => "osoe", -selectmode= +>"extended", )->pack(-side => 'left', -fill => 'y'); my $lb->insert("end", @tags_list); my $lb2 = $f1->Scrolled("Listbox", -scrollbars => "osoe", -selectm +ode=>"extended", )->pack(-side => 'left', -fill => 'y'); my $sel_btn=$f2->Button(-text => "> Select Field", -command => \&select_tag)->pack(-side =>'left', -expand=>1, + -anchor => 'center'); $balloon->attach($sel_btn, -msg => "Select field"); + my $sel1_btn= $f2->Button(-text => "> Unselect Field", -command => \&unselect_tag)->pack(-side =>'left', -expand= +>1, -anchor => 'center'); $balloon->attach($sel1_btn, -msg => "Unselect field"); my $sel2_btn= $f2->Button(-text => ">> Select all", -command => \&select_all)->pack(-side =>'left', -expand=>1 +, -anchor => 'center'); $balloon->attach($sel2_btn, -msg => "Select all"); my $sel3_btn= $f2->Button(-text => "<< Unselect All Fiel +ds", -command => \&unselect_all)->pack(-side =>'left', -expand= +>1, -anchor => 'center'); $balloon->attach($sel3_btn, -msg => "Unselect all fields") +; my $convert_btn=$f3->Button(-text=>"Go", -command=> sub { my $selected_tags = $lb2->get(0,'end'); if (scalar @$selected_tags < 1){ my $msg_title = "ListBox selection Error"; my $message = "Please make a selection first!"; message_box($msg_title,$message); } ##print first line with field names, tabbed $list_selected_tags = join ",", @$selected_tags; $t->insert('end', $list_selected_tags); $t->insert('end', "\n"); foreach my $k (sort keys %$users_ref){ foreach my $i (@$selected_tags){ my $temp_i = $users_ref->{$k}{$i}; if ($temp_i =~ /,/){ $temp_i = "\"$temp_i\""; } else{ $temp_i = "$temp_i"; } #$temp_i =~ s/"/""/g; $t->insert('end', "$temp_i,") unless $i eq @$selecte +d_tags[-1];; $t->insert('end', "$temp_i\n") if $i eq @$selected_t +ags[-1]; } #$t->insert('end', "\n"); } $tl21->withdraw; } )->pack(-side =>'left', -expand=>1, -anchor => 'center'); $balloon->attach($convert_btn, -msg => "Filter report"); my $cancel_btn=$f3->Button(-text=>"Cancel", -command=> sub { $tl21->withdraw }#end of 2nd inner sub )->pack(-side =>'left', -expand=>1, -anchor => 'center'); $balloon->attach($cancel_btn, -msg => "Close current window" +); sub select_tag { my @elements = $lb->curselection; my $delete_point; if ($#elements>=0){ foreach my $i (0..$#elements) { $delete_point =$elements[0]; my $element = $lb->get($elements[$i]); $lb2->insert("end","$element LALA") ; } $lb->delete($delete_point,$elements[-1]); $lb->selectionClear(0,'end'); } else { my $msg_title = "ListBox selection Error"; my $message = "Please make a selection first!"; message_box($msg_title,$message); } } sub unselect_tag { my @elements = $lb2->curselection; my $delete_point; if ($#elements>=0){ foreach my $i (0..$#elements) { $delete_point =$elements[0]; my $element = $lb2->get($elements[$i]); $lb->insert("end","$element") ; } $lb2->delete($delete_point,$elements[-1]); $lb2->selectionClear(0,'end'); } else{ my $msg_title = "ListBox selection Error"; my $message = "Please make a selection first!"; message_box($msg_title,$message); } } sub select_all{ $lb2->delete(0,'end'); $lb2->insert(0,@tags_list); $lb->delete(0,'end'); } sub unselect_all{ $lb->delete(0,'end'); $lb->insert(0,@tags_list); $lb2->delete(0,'end'); } }#end of if (! Exists($tl21))statement else{ $tl21->deiconify(); $tl21->raise(); } }

In reply to TK::MListbox Issue by Anonymous Monk

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.