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

I have a question and I am sure it is something very small. I am wondering if someone can help me. I am trying to create a Scrolled Listbox (initial width 25) and once options are populated in the Listbox the width of the Listbox will change based on the widest option. I know if you set width to 0 my Listbox will be resized to the widest option (expected behavior), but this is not working :-\

# Create a frame ... my $suite_fr = $page->Frame->pack(-expand => 1, -fill => 'both'); # Add a label to the frame $suite_fr->Label(-text => 'Suite:')->pack(-side => 'left'); # create a Scrolled Listbox $self->{suite_lb} = $suite_fr->Scrolled('Listbox', -exportselection => '0', -width => '25', -height => '7', -scrollbars => 'e', -selectbackground => 'gray80', -selectforeground => 'black', -selectmode => +'extended' )->pack(-side => 'left'); # A user will then click the submit buttom and be redirected to anothe +r method with the code listed below .. # Populate the ListBox with the valid suites $self->{suite_lb}->insert(0, @list); # Retrieve the Listbox subwidget that is part of Scrolled my $lb_subwidget = $self->{suite_lb}->Subwidget("listbox"); # Change the size of the Listbox $lb_subwidget->configure(-width => '0');

However, this does not work. If I create a plan Listbox then I can resize it using configure() but, there is something different about using the Scrolled method that is different. Please help !

Replies are listed 'Best First'.
Re: Problem setting width on Scrolled Listbox
by thundergnat (Deacon) on May 26, 2010 at 15:26 UTC

    Assuming this is using Tk, you need to set pack propagate on for the Scrolled container. See below.

    use warnings; use strict; use Tk; my $page = MainWindow->new; $page->geometry('600x200'); # Create a frame ... my $suite_fr = $page->Frame->pack(-expand => 1, -fill => 'both'); # Add a label to the frame $suite_fr->Label(-text => 'Suite:')->pack(-side => 'left'); # create a Scrolled Listbox my $suite_lb = $suite_fr->Scrolled( 'Listbox', -exportselection => '0', -height => '7', -scrollbars => 'e', -selectbackground => 'gray80', -selectforeground => 'black', -selectmode => 'extended' )->pack(-side => 'left'); my $button = $page->Button( -text => ' Fill ', -command => sub{ fillbox($suite_lb) }, )->pack; MainLoop; sub fillbox{ my $self = shift; $self->packPropagate(1); my @list = 'X' x rand 80; # Populate the ListBox with the valid suites $self->insert(0, @list); $self->Subwidget("listbox")->configure(-width => 0); }
Re: Problem setting width on Scrolled Listbox
by amedico (Sexton) on May 26, 2010 at 14:41 UTC
    You might want to specify what toolkit you're using - Tk? Wx? Something else?