Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Tk scrollbar

by Perluser (Novice)
on Oct 27, 2004 at 22:28 UTC ( [id://403170]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, The following code displays a listbox and a scrollbar. I would like the scrollbar to have the same height as the listbox, but it is shown shrinked. I guess I am missing something. Suggestions?

PS: use of Scrolled method is out of question.
#!/usr/bin/perl -w use Tk; $top = new MainWindow; $f = $top->Frame; $f->Label(-text=>"List")->grid('x'); $f->Listbox(-height=>20)->grid($f->Scrollbar()); $f->pack(-side=>'left'); MainLoop;

Replies are listed 'Best First'.
Re: Tk scrollbar
by kvale (Monsignor) on Oct 27, 2004 at 22:58 UTC
    I would stick with pack() as a geometry manager, unless you need something special. Here is an example of a listbox with an attached scrollbar, including the linking logic:
    #!/usr/bin/perl -w use Tk; $top = new MainWindow; $f = $top->Frame; $label = $f->Label(-text=>"List"); $scrollbar = $f->Scrollbar(); $listbox = $f->Listbox( -yscrollcommand => ['set' => $scrollbar]); $scrollbar->configure( -command => ['yview' => $listbox]); $label->pack( -side => 'top', -fill => 'x'); $scrollbar->pack( -side => 'right', -fill => 'y'); $listbox->pack( -side => 'left', -fill => 'both'); $f->pack(-side=>'left'); MainLoop;

    -Mark

Re: Tk scrollbar
by pg (Canon) on Oct 27, 2004 at 23:16 UTC

    Why not just use Scrolled ;-?

    use Tk; $top = new MainWindow; $f = $top->Frame->pack(); $f->Scrolled("Listbox", -height=>20, -scrollbars => "e")->pack(-side=> +'left'); MainLoop;
      My original post was a greatly simplified version. A bit more complicated is this: I have to use grid because with two listboxes below I need labels to stick out nicely right above corresponding lists. And I want one scrollbar to scroll both lists:
      $top = new MainWindow; $f = $top->Frame; $f->Label(-text=>"1")->grid('x',$f->Label(-text=>"2")); $f->Listbox()->grid($f->Scrollbar(),$f->Listbox()); $f->pack(-side=>'left'); MainLoop;
      Even with one Listbox it appeared that I cannot control how scrollbar is shown in grid. Or, can I?

        To get your widgets to stretch using the grid manager, you need to specify the -sticky attribute. Basically for a given widget, you tell grid which sides (n,s,e,w) should "stick", and it makes sure those sides of the widget always touch the appropriate border of the widget's grid cell. In this case, you want the scrollbar to extend all the way from the top of the cell to the bottom, so you want the top (n) and bottom (s) of the widget to stick:

        $f->Listbox()->grid($f->Scrollbar(), -sticky => 'ns');

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://403170]
Approved by kvale
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found