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

I can not figure out how to put a scrollbar on a Listbox I have search and all they say
is to use -yscrollcommand.
$Search_Listbox = $mw->Listbox( -background => "white", -yscrollcommand => 1, -height => 7, -width => 47, -listvariable => \@Test); $Search_Listbox->place(-x => 5, -y => 150);

Could someone help me out please. The @test array has 10 items in it.

Replies are listed 'Best First'.
Re: TK ListBox Scrolling
by zentara (Cardinal) on Mar 06, 2008 at 15:56 UTC
    For most Tk widgets, you should use the Scrolled method, which automatically adds scrollbars correctly. The only time you try to manually add a yscrollcommand is to do something tricky with the scrollbar.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->title("Listbox"); # For example purposes, we will use one word for each letter my @choices = qw/alpha beta charlie delta echo foxtrot golf hotel india juliet kilo lima motel nancy oscar papa quebec radio sierra tango uniform victor whiskey xray yankee zulu/; #Create the text box and insert the list of choices in to it my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe", -height => 10, -width => 10, -selectmode => "multiple"); my $real_lb = $lb->Subwidget('scrolled'); $real_lb->configure(-borderwidth=>0); $lb->insert("end", sort @choices); $lb->pack(-side => "left"); $mw->Button(-text => "Exit", -command => sub{exit; })->pack(-side => "bottom"); $mw->Button(-text=>"View", -command => sub { foreach ($lb->curselection()) { print "$choices[$_]\n"; } my $w = $real_lb->width; print "$w\n"; my $sample = ' ' x 10; my $font_len = $lb->fontMeasure('default', $sample ); print "$font_len\n"; } )->pack(-side => "bottom"); $lb->selectionSet('end'); $lb->see('end'); MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thank you for the replys,
      I went with zentara suggestion. and changed the code to the following:
      $Search_Listbox = $mw->Scrolled("Listbox", -background => "white", -scrollbars => "osoe", -height => 7, -width => 47, -listvariable => \@Test); $Search_Listbox->place(-x => 5, -y => 150);

      which solved all my problems.
      *** Case Closed ***
      Hi. This post really helped me with an unrelated issue, and by using see -> ('end') allowed me to watch a ton of output scroll in my textbox without having to wait for completion. Where is there other documentation on internal functions, such as "see" that I can reference? Thanks
Re: TK ListBox Scrolling
by pc88mxer (Vicar) on Mar 06, 2008 at 15:52 UTC
    I did a Google search on "tk listbox scrollbar" (without the quotes) and found this example in Python (the first returned result):
    frame = Frame(master) scrollbar = Scrollbar(frame, orient=VERTICAL) listbox = Listbox(frame, yscrollcommand=scrollbar.set) scrollbar.config(command=listbox.yview) scrollbar.pack(side=RIGHT, fill=Y)listbox.pack(side=LEFT, fill=BOTH, e +xpand=1)
    Translation to perl should be straight-forward. Basically, you need to create a Scrollbar widget, configure its command to notify the Listbox, and configure the Listbox to notify the Scrollbar via the Scrollbar's set method. Try this out, and let us know if it works.
      I found that example on my search too, I just figured that the Listbox would have an option
      without having to do the above. But I will give it a wack and post back in a few