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

Hello All, I have two questions reguarding Tk::ListBox.
Issue:
I create a list box with "-state->disabled" but it does not show the items in the list box
Questions
1. How do I create a list box,in a disabled state, so that the items in the listbox are shown but the user is not able to select anything in the list?
2. When I click the checkbox, how do I enable the listbox so that the user is now able to select items from the list?
Thanks for your help and assistance in advance
Gary
use strict; use Tk; my $lst_properties; my $chk_options; my $chkoptions; my $MainWindow; &maingui; sub maingui { $MainWindow = MainWindow->new(-title=>'Listbox'); $chk_options = $MainWindow -> Checkbutton ( -anchor=>'nw', -justify=>'left',-relief=>'flat', -indicatoron=>1,-text=>'Additional Criteria', -variable=>\$chkoptions, -state=>'normal', -command=>\&Additional_Criteria ) -> grid(-row=>1,-column=>0,-pady=>5); $lst_properties= $MainWindow -> Scrolled ( 'Listbox', -selectmode=>'single', -relief=>'sunken', -scrollbars=>'osoe',-width=>50,-state=>'disabled'); $lst_properties -> insert('end',"People","Animals","Aliens","Meat" +, "Fruit","Vegies","Dairy","Pultry", ); $lst_properties -> grid(-row=>5,-column=>1,-pady=>3); MainLoop; } sub Additional_Criteria { #code her to test if check box is selected or not if ($chkoptions) { print "Enable\n"; #Code here to ENABLE ListBox } else { print "Disable\n"; #Code here to DISABLE ListBox } }

Replies are listed 'Best First'.
Re: Tk::ListBox questions
by zentara (Cardinal) on Mar 17, 2008 at 13:51 UTC
    This works, use the listvariable option instead of insert.
    #!/usr/bin/perl use strict; use Tk; my $lst_properties; my $chk_options; my $chkoptions; my $MainWindow; &maingui; sub maingui { $MainWindow = MainWindow->new(-title=>'Listbox'); my @properties = ("People","Animals","Aliens","Meat", "Fruit","Vegies","Dairy","Pultry", ); $chk_options = $MainWindow -> Checkbutton ( -anchor=>'nw', -justify=>'left',-relief=>'flat', -indicatoron=>1,-text=>'Additional Criteria', -variable=>\$chkoptions, -state=>'normal', -command=>\&Additional_Criteria ) -> grid(-row=>1,-column=>0,-pady=>5); $lst_properties= $MainWindow -> Scrolled ( 'Listbox', -selectmode=>'single', -relief=>'sunken', -scrollbars=>'osoe', -width=>50, -listvariable=>\@properties, -state=>'disabled' ); $lst_properties -> grid(-row=>5,-column=>1,-pady=>3); MainLoop; } sub Additional_Criteria { #code her to test if check box is selected or not if ($chkoptions) { print "Enable\n"; $lst_properties->configure(-state=>'normal'); } else { print "Disable\n"; #Code here to DISABLE ListBox $lst_properties->configure(-state=>'disabled'); } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hello Zentara,
      This worked! I definatly like the idea of "listvaraibles".
      Thanks for your assistance
        Yeah, the nice thing about an array as a list variable, is you can just modify the array any way you want, and it will be automatically updated in the listbox. So you can sort, remove, or splice elements into the array at any position, and the list will show it. Trying to modify the list itself is quite abit trickier, since you need to deal with the indices of the listbox.

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Tk::ListBox questions
by moritz (Cardinal) on Mar 17, 2008 at 12:31 UTC
    2. When I click the checkbox, how do I enable the listbox so that the user is now able to select items from the list?

    With $listbox->configure(-state => 'normal'); in the event handler of that checkbox.

      I tried adding the following in the subroutine, but it had no effect
      sub Additional_Criteria { #code her to test if check box is selected or not if ($chkoptions) { print "Enable\n"; $lst_properties->configure(-state=>'normal'); } else { print "Disable\n"; #Code here to DISABLE ListBox } }
Re: Tk::ListBox questions
by moritz (Cardinal) on Mar 17, 2008 at 13:53 UTC
    Regarding the first question: you can first insert the items, then disable the list box. That way the items will be displayed:
    use strict; use warnings; use Tk; my $w = MainWindow->new(); my $l = $w->Listbox()->pack(); for (qw(foo bar baz)){ $l->insert('end', $_); } # disable it afterwards $l->configure(-state => 'disabled'); MainLoop;

    I don't see why $l->configure(-state => 'normal'); or $l->configure(-state => 'disabled'); shouldn't work for enabling/disabling the box. Try to insert a few prinit statements to verify that you're actually executing the code you think you are executing.

      Hello moritz,
      That was it. I overlooked, "If the listbox is disabled then items may not be inserted or deleted". I implemented suggestion to make it normal, add the list and then disable it.
      Thanks again
Re: Tk::ListBox questions
by zentara (Cardinal) on Mar 17, 2008 at 13:44 UTC
    How do I create a list box,in a disabled state, so that the items in the listbox are shown but the user is not able to select anything in the list?

    Well a single or double click on an enabled list doesn't do much, until you bind the double click

    $lb -> bind("<Double-Button-1>",\&display); sub display { $selected = $lb -> get($lb -> curselection); $message = "You selected: $selected \n"; print "you chose $selected\n"; $lb->selectionClear(0, 'end'); }
    As far as the single click selection goes, it is deeply built into the Listbox widget. You may be able to subclass the Listbox, and redefine what a single click does, search groups.google for " perl tk listbox derived". But the easiest thing, may be to change the various color options, so that "it appears" they are not doing anything, and just discard any selections. In other words, have the selected color set to be the same as the unselected color. Otherwise, it may be better to choose a different widget.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum