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

Hi,

I'm using Tk::Table and I'm having a problem with the scrollbars. I want to have a scrollbar appear only on the right-hand side, and only if there is a 'real' need to scroll (as the Tk::Scrolled pod puts it). This would mean using the 'oe' anchor for the -scrollbars option. This I have done (see code), but I still get the scrollbar, even when it is not needed. Does anyone know why? If there are alternative solutions, keep in mind that I need/want that top non-scrolling row of column headers to still work as designed.

here's the code:

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Table; my $cols = 6; my $rows = 10; # create the main window and frame my $mw = new MainWindow(); my $frame = $mw->Frame()->pack; my $table = $frame->Table( -columns => $cols, -rows => $rows + 1, -fixedrows => 1, -scrollbars => 'oe', -relief => 'raised', ); # column headings for my $c(1 .. $cols){ my $tmp = $table->Label(-text => 'Row '.$c, -width => 6, -relief => 'raised', ); $table->put(0,$c,$tmp); } # populate the cells and bind an action to each one for my $r(1 .. $rows){ for my $c(1 .. $cols){ my $tmp = $table->Label( -text => $r.','.$c, -padx => 2, -anchor => 'w', -background => 'white', -relief => 'groove', ); $table->put($r,$c,$tmp); } } $table->pack(-expand => 'yes',-fill => 'both'); MainLoop();

Thanks!

Replies are listed 'Best First'.
Re: Tk::Table and optional scrollbar
by Khen1950fx (Canon) on Jul 26, 2012 at 21:35 UTC
    Try this:
    #!/usr/bin/perl use strict; use autodie; use warnings; use Tk; use Tk::Table; my $cols = 6; my $rows = 10; my $mw = new MainWindow(); my $frame = $mw->Frame()->pack; my $table = $frame->Table( -columns => $cols, -scrollbars => 'oe', -rows => $rows + 1, -fixedrows => 10 + 1, -fixedcolumns => 10, -relief => 'raised', ); for my $c ( 1 .. $cols ) { my $tmp = $table->Label( -text => 'Row ' . $c, -width => 6, -relief => 'raised', ); $table->put( 0, $c, $tmp ); } for my $r ( 1 .. $rows ) { for my $c ( 1 .. $cols ) { my $tmp = $table->Label( -text => $r . ',' . $c, -padx => 2, -anchor => 'w', -background => 'white', -relief => 'groove', ); $table->put( $r, $c, $tmp ); } } $table->pack( -expand => 'yes', -fill => 'both' ); MainLoop();

      Try this:

      Why? It doesn't show any scrollbars at all, no matter how you resize the window

Re: Tk::Table and optional scrollbar
by zentara (Cardinal) on Jul 27, 2012 at 13:08 UTC
    I would try putting $table into a Scrolled Pane, rather than a Frame. Frames don't deal with Scrolling well, but Panes do.
    #!/usr/bin/perl use strict; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry( '400x250' ); my $mwf = $mw->Scrolled( 'Pane', -scrollbars => 'ose', -sticky => 'nwse', )->pack( -expand => 1, -fill => 'both' ); my $f1 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my $f2 = $mwf->Frame()->pack( -expand => 1, -fill => 'both' ); my %dudes; my $dude_count = 1; foreach my $fr($f1, $f2){ foreach (1..6){ $dudes{$dude_count}{'text'} = $fr->Text( -background => 'white', -foreground => 'black', -height => 10, -width => 22 )->pack(-side=>'left', -expand => 1, -fill => 'both' ); $dudes{$dude_count}{'text'}->insert('end', "\n Dude $dude_count\n") +; $dude_count++; } } MainLoop();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Frames don't deal with Scrolling well, but Panes do.

      Thats funny, because what Scrolled does, is create a Frame with scrollbars, and puts your widget (Pane) inside, and Tk::Table is-a Tk::Frame

        If you read "perldoc -m Tk::Pane" there is alot more being done that just creating a Frame with Scrollbars. All I can say is that Scrolled Panes respond better than Scrolled Frames.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
      that is tempting, zentara, but I lose that non-scrolling header row.
        my $header = $mw->Pane->pack( -side => 'top' ); my $rest = $mw->Scrolled ... ->pack;
Re: Tk::Table and optional scrollbar
by Anonymous Monk on Jul 26, 2012 at 22:53 UTC

    Does anyone know why?

    Because Tk::Table is written that way -- don't know if Tk::Table considers that a bug

    See sub Tk::Widget::Scrolled and sub Tk::Frame::AddScrollbars

    Maybe employ Devel::TraceCalls to maybe figure out where "oe" hiding happens, where to modify Tk::Table