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

I have created a Listbox wig. and tried to add a Scrollbar Wig to it. The listbox has a Height of 7.
I get the Scrollbar to show up but it is not the right size I want it to be the same size as the Listbox. Not sure what I need to do.
Search_Listbox_Scrollbar = $mw->Scrollbar(); $Search_Listbox_Scrollbar->place(-x => 295, -y => 190); $Search_Listbox = $mw->Listbox( -background => "white", -yscrollcommand => ['set' => $Search_Listbox_Scrollbar], -height => 7, -width => 47); $Search_Listbox_Scrollbar->configure(-command => ['yview' => $Search_L +istbox]); $Search_Listbox->place(-x => 5, -y => 150);

Replies are listed 'Best First'.
Re: TK ListBox and Scrollbar
by johngg (Canon) on Mar 10, 2008 at 21:12 UTC
    How about creating a scrolled listbox rather than separate scrollbar and listbox.

    my $Scrolled_Search_Listbox = $mw->Scrolled( q{Listbox}, -background => "white", ... );

    The following code is from an application with scrolling listboxes that were simple to implement.

    I hope this is helpful.

    Cheers,

    JohnGG

      When I use Scrolled It doesn't want to updates the scroll when I load a lot of
      data into the listbox.
        I've knocked up this little app. which inserts ten items into the listbox every time you hit the <Insert> button. It seems to update the scroll bar ok on my system, Sun Ultra 30, Solaris 9, perl 5.8.4, Tk-804.027.

        use strict; use warnings; use Tk; my $rcGetTen = do { my $count = 0; sub { my @arr; push @arr, q{Item } . ++ $count for 1 .. 10; return @arr; }; }; my $bgColour = q{LightSteelBlue3}; my %labelColours = ( -foreground => q{NavyBlue}, -background => q{LemonChiffon} ); my %buttonColours = ( -background => q{grey35}, -foreground => q{yellow2}, -activebackground => q{grey45}, -activeforeground => q{yellow}, -disabledforeground => q{grey55} ); my %commonListboxOpts = ( -setgrid => 1, -scrollbars => q{e}, -selectmode => q{single}, -selectforeground => q{grey35}, -selectbackground => q{AntiqueWhite}, -exportselection => 0 ); my $mainWin = MainWindow->new( -background => $bgColour, ); $mainWin->title(q{scrollListbox}); my $listboxFrame = $mainWin->Frame( -relief => q{flat}, -background => $bgColour, -borderwidth => 2, ); $listboxFrame->pack( -side => q{top}, ); my $listbox = $listboxFrame->Scrolled( q{Listbox}, %commonListboxOpts, -width => 15, -height => 6, -setgrid => 1, ); $listbox->pack( -padx => 5, -pady => 5, -fill => q{y}, -expand => 1, -anchor => q{n}, ); my $controlButtonFrame = $mainWin->Frame( -relief => q{flat}, -background => $bgColour, -borderwidth => 2, ); $controlButtonFrame->pack( -side => q{top}, ); my $quitButton = $controlButtonFrame->Button( %buttonColours, -width => 6, -text => q{Quit}, -command => sub {exit;}, ); $quitButton->pack( -side => q{right}, -padx => 5, -pady => 5, ); my $insertButton = $controlButtonFrame->Button( %buttonColours, -width => 6, -text => q{Insert}, -command => sub { $listbox->insert( q{end}, $rcGetTen->() ) }, ); $insertButton->pack( -side => q{right}, -padx => 5, -pady => 5, ); MainLoop();

        I hope this is of interest.

        Cheers,

        JohnGG

Re: TK ListBox and Scrollbar
by zentara (Cardinal) on Mar 11, 2008 at 12:21 UTC
    Well johngg is right to point you to the scrolled widget. However, if you really need to make your own scrollbar ( there are certain times you may need to do it), it is easier with pack than place. With pack you tell the scrollbar to fill in it's direction, like
    $yscroll->pack( -side => 'left', -fill => 'y' ); $xscroll->pack( -side => 'bottom', -fill => 'x' );
    I don't see 'fill' as an option to Tk::place, but if you read perldoc Tk::place, there are some options you can play with to try and make it the same size as the listbox, I would try the -relheight and -relwidth options. But pack is so much easier to use than place, so unless you need it for some reason, switch to pack, or use the Scrolled('Listbox'...)

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      You got me thinking when you said look into tk::place. I started reading
      and found that you can change the height and width of the wig with place
      Thank you for the reply

      *** Problem Solved ***

      For anyone who reads this later on all I did was this:
      $Search_Listbox_Scrollbar->place(-x => 295, -y => 150, -height => 102) +;