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

Hello Monks,

Does anyone know if it is possible to change the height of a listbox after it has been created?

I tried to use the configure method to change the -height option, but that seems to have no effect. Curiously, subsequent calls to cget return the new height value, even though the actual height has not changed. My test program is below.

Can anyone help me understand what is going on? Thanks ...

use strict; use warnings; use Tk qw(MainLoop); my $mw = MainWindow->new(-title => " Listbox Height-Change Test"); my $myFont = "{Lucida Fax} 12 bold"; my ($lb, $lbw); $mw->Button(-text => 'Print Height', -font => $myFont, -command => sub { print "HEIGHT: ", $lb->cget(-height), "\n"; } +, )->pack(); $mw->Button(-text => 'Change Height', -font => $myFont, -command => sub { $lb->configure(-height=>5); }, )->pack(); my @items = ("thing1", "THing2", "THING3"); $lb = $mw->Scrolled('Listbox', -font => $myFont, -background => 'darkblue', -foreground => 'white', -scrollbars => 'osoe', -height => 15, -width => 28, -borderwidth => 1, -relief => 'solid', )->pack(); # $lbw = $lb->Subwidget('listbox'); $lb->delete(0,'end'); my $num = 0; $lb->insert('end', sprintf("%3d. $_\n", ++$num)) for (@items); MainLoop();

Replies are listed 'Best First'.
Re: Seeking way to dynamically change listbox height
by choroba (Cardinal) on Sep 04, 2021 at 22:18 UTC
    Interestingly, if you create a Listbox that's not scrolled, it works correctly.

    I wasn't able to make it change the height even with

    sub change_height { my ($widget, $height) = @_; print "Configuring $widget\n"; eval { $widget->configure(-height => $height) }; for my $subwidget ($widget->Subwidget) { change_height($subwidget, $height) if $subwidget; } } $mw->Button(-text => 'Change Height', -font => $myFont, -command => sub { change_height($lb, 5); }, )->pack();

    I have no idea what to try next.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Seeking way to dynamically change listbox height
by CrashBlossom (Beadle) on Sep 04, 2021 at 23:24 UTC

    Looks like the trick is to manually add the scrollbars rather than using the Scrolled method. Problem solved (i think).

    use strict; use warnings; use Tk qw(MainLoop); my $mw = MainWindow->new(-title => " Listbox Height-Change Test"); my $myFont = "{Lucida Fax} 12 bold"; my $lb; $mw->Button(-text => 'Change Height', -font => $myFont, -command => sub { $lb->configure(-height=>5); }, )->pack(); my @items = ("thing1", "THing2", "THING3"); my $scrollbar = $mw->Scrollbar( ); $lb = $mw->Listbox( -yscrollcommand => ['set' => $scrollbar], -font => $myFont, -background => 'darkblue', -foreground => 'white', -height => 15, -width => 28, -borderwidth => 1, -relief => 'solid', ); $scrollbar->configure(-command => ['yview' => $lb]); $scrollbar->pack(-side => 'right', -fill => 'y'); $lb->pack(); $lb->delete(0,'end'); my $num = 0; $lb->insert('end', sprintf("%3d. $_\n", ++$num)) for (@items); MainLoop();
Re: Seeking way to dynamically change listbox height
by CrashBlossom (Beadle) on Sep 05, 2021 at 01:04 UTC

    Apparently the Scrolled method creates a frame into which the listbox and scrollbar widgets are placed. I'm guessing that the frame somehow gets in the way of changing the listbox dimensions.

    choroba's observation that a plain listbox could be modified inspired me to investigate further, leading to the solution I mentioned above. So thanks, choroba!

Re: Seeking way to dynamically change listbox height
by Anonymous Monk on Sep 05, 2021 at 00:47 UTC