perl_seeker has asked for the wisdom of the Perl Monks concerning the following question:

Hello

Here is a section of my perl program that uses the Tk module. I have two listboxes in the same window
sub meter{ my $metw = MainWindow-new; $metw->geometry("500x500"); $metw->title("Meter work details"); $metw->configure(-background=>'purple'); my $frame8=$metw->Frame(-relief=>'groove', -borderwidth=>3, -backgrou +nd=>'blue',)->pack(-side=>'top', -pady=>15, -padx=>15); my $label8=$frame8->Label(-text=>'Enter consumer name',-background=>'b +lue', -foreground=>'white',)->pack(-side=>'left'); $entry8=$frame8->Entry(-width=>30,-background=>'white')->pack(-side=>' +left', -pady=>3); ($label10 = $metw->Label(-text=>'Double click on subdivision name',-ba +ckground=>'blue', -foreground=>'white'))->pack(-side=>'top',-pady=>2) +; $sd_list = $metw->Listbox(-exportselection => 0)->pack; $sd_list->insert('end', "loc1","loc2","loc3","loc4","loc5","loc6", "lo +c7", "loc8", "loc9","loc10","loc11","loc12","loc13"); $sd_list->bind('<Double-1>', \&meter_subdivision); sub meter_subdivision { my $sd = $sd_list->get('active'); return $sd; } ($space16 = $metw->Label(-text=>' ',-background=>'purple') +)->pack; ($space17 = $metw->Label(-text=>' ',-background=>'purple') +)->pack; ($labeln = $metw->Label(-text=>'Double click on appropriate option',-b +ackground=>'blue', -foreground=>'white'))->pack(-side=>'top',-pady=>2 +); $nr_list = $metw->Listbox(-listvariable => $_, -exportselection => 0)- +>pack; $nr_list->insert('end',"New service","Replacement"); $nr_list->bind('<Double-1>',\&type_meter_work); sub type_meter_work { my $tmw = $nrd_list->get('active'); return $tmw; } ($space8 = $sw->Label(-text=>' ',-background=>'purple'))-> +pack; ($space9 = $sw->Label(-text=>' ',-background=>'purple'))-> +pack; my $label7=$metw->Label(-text=>'Enter phase : single/three',-backgroun +d=>'blue', -foreground=>'white',)->pack(-side=>'top'); $entry7=$metw->Entry(-width=>30,-background=>'white')->pack(-side=>'to +p', -pady=>3); $space10 = $sw->Label(-text=>' ',-background=>'purple'))-> +pack; $space11 = $sw->Label(-text=>' ',-background=>'purple'))-> +pack; $metw->Button(-text=>'Print receipt',-background=>'blue',-command=>\&m +eter_fwrite_print)->pack(-side=>'top',-pady=>2); }
When i try to print the selected items in both the lists in another subroutine, i get the following error:
Tk::Error: Can't call method "get" on an undefined value at C:\Perl\pr +ograms\cash_counter.pl line 167. <Double-Button-1> (command bound to event)
If i have only one listbox in the window the value of the selected item is printed just fine.

In fact i would like to replace the"Enter phase" label with a list box too to reduce typing for the user.So three listboxes in one window.

Also how to use a single click instead of double click for selection.Will this cause any problems?

Please help.

Replies are listed 'Best First'.
Re: Perl/Tk listbox question
by lamprecht (Friar) on Aug 13, 2009 at 11:08 UTC
    Hi,

    I don't see where you define $nrd_list in your code. Do you use strict; and use warnings; ? Are you sure you want to get('active')? Could  curselection() be more appropriate?

    You will get the best help if you post a short stripped down runnable example that demonstrates your problem.


    Cheers, Christoph
      Thank you,

      it was the $nrd_list causing the problem (typo) . Silly of me. I needed my $tmw = $nr_list->get('active'); and i was using my $tmw = $nrd_list->get('active');

      get('active') is working fine.

      Thanks again, cheers