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

Dear Perl Monks,
i am using Tk::JComboBox.
i can able to selecting first and second combo's data for the first time,later i can not select the second combo box
if i use TK::BrowseEntry it is working fine
please refer my code
use Tk; use Tk::JComboBox; my $ScreenSize = "300x200+0+0"; &Screen; MainLoop; sub Screen { $MainScreenObject = MainWindow->new(-background =>"white"); $MainScreenObject->geometry($ScreenSize); @TestList = ('12','123','12345'); $test= createComboBox('Test',5,100,50); $test1= createComboBox('Test1',5,100,100); } sub onTestComboBoxSelect { @TestList1 = ('1','3','125'); $test1->configure(-choices => \@TestList1 ,-state => 'normal'); + } sub onTest1ComboBoxSelect { } sub createComboBox { my($ComboBoxName,$Width,$x,$y) = @_; $ComboBoxName =~ s/\s+//g; my $ComboBoxObject = $MainScreenObject->JComboBox(-background =>'w +hite', -textvariable => \${$ComboBoxName}, -choices => \@{$ComboBoxName."List"}, -browsecmd =>\&{"on".$ComboBoxName."ComboBoxSelect"} +, -entrywidth =>$Width,)->place(-x=>$x,-y=>$y); return($ComboBoxObject); }
please help me on this

Replies are listed 'Best First'.
Re: problem with Tk::JComboBox
by zentara (Cardinal) on Sep 22, 2008 at 13:16 UTC
    First , put at the top of your script:
    use warnings; use strict;
    Then you will probably see your code in
    -textvariable => \${$ComboBoxName}, -choices => \@{$ComboBoxName."List"}, -browsecmd =>\&{"on".$ComboBoxName."ComboBoxSelect"}
    is a very bad way of tracking names, and is causing your problem. Probably what is happening, is $ComboBoxName can't be relied on the way you are using it without strict. It is getting changed somewhere , probably in the -textvariable, and gets undefined or emptied.

    I won't even bother trying to figure out the problem, because the way you are naming things goes against every good programming practice for handling variables. I'm not sure what you are trying to do in the broswecmd, but this is how I would setup your program, using a hash to store the boxes. But there seems to be a bug in this too, if you browse the lower box first it changes only itself, but if you browse the upper box first, it changes both boxes. ???? A bug? Too early on Monday morning? Weird.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JComboBox; my %box; my $ScreenSize = "300x250+50+50"; my $MainScreenObject = MainWindow->new(-background =>"white"); $MainScreenObject->geometry($ScreenSize); my @TestList = ('12','123','12345'); $box{1}{'var'} = 1; $box{1}{'obj'} = createComboBox(1,5,100,50); $box{2}{'var'} = 2; $box{2}{'obj'} = createComboBox(2,15,100,150); MainLoop; sub ComboBoxSelect { my $list = shift; print "Browsing $list\n"; my @TestList1 = ('a','b','cde'); $box{$list}{'obj'}->configure(-choices => \@TestList1 ,-state => 'no +rmal'); } sub createComboBox { my($ComboBoxName,$Width,$x,$y) = @_; my $ComboBoxObject = $MainScreenObject->JComboBox( -background =>'white', -textvariable => \$box{$ComboBoxName}{'var'}, -choices => \@TestList, -browsecmd => [\&ComboBoxSelect,$ComboBoxName], -entrywidth =>$Width,)->place(-x=>$x,-y=>$y); return($ComboBoxObject); }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: problem with Tk::JComboBox
by jethro (Monsignor) on Sep 22, 2008 at 16:09 UTC

    Zentara already said the important things. But I think it really was your intention to have the selection of the second widget change when the first widget is selected as in your script. In that case you only need to change Zentaras script this way:

    sub ComboBoxSelect { my $list = shift; print "Browsing $list\n"; if ($list == 1) { my @TestList1 = ('a','b','cde'); $box{2}{'obj'}->configure(-choices => \@TestList1,-state => 'norma +l'); } }

    If you prefer to have separate browsecmd functions for your widgets, you still can do that with Zentaras version. Just give a subroutine reference as a fifth parameter to createComboBox

    UPDATE: The original program of AnonMonk works when the line @TestList1 = ('1','3','125'); is moved out of the callback routine to the end of the Screen routine. Looks like a bug in JComboBox to me

      I wonder why you can't have a common browsecmd, and pass in a parameter, without them interfereing with one another.....seems like a bug. JComboBox must be doing something globally in the module.

      I'm not really a human, but I play one on earth Remember How Lucky You Are
        Which interference are you refering to? I just installed JComboBox and tested all the scripts superficially and they seemed to work as expected (Apart from the bug(?) with @TestList1 in the callback)