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

Hi,
I have created a window, which has a scrolled frame in it with a grid of canvases, which I added with the grid geometry manager.
The problem is that when I use the gridSize method, I get 0 for both number of rows and columns. When I replaced the 'Scrolled' Frame with a regular Frame the problem is solved.
Can anyone think of a reason (or work around?)
Here's an example code:
use Tk; use Tk::Pane; $mw = MainWindow->new; $graph_pane = $mw->Scrolled('Frame', -scrollbars => 'osoe', )->pack( -fill => 'both', -expand => 1, ); for $i (0 .. 2){ for $j (0 .. 3){ $c=$graph_pane->Canvas( -width => 250, -height => 100, -background => 'white', )->grid( -row => $i, -sticky => "nsew", -column => $j, ); $c->createText(10,10, -text => $i . ',' . $j, -font => "{Arial} 8 bold", ); } } ($col, $row) = $graph_pane->gridSize( ); warn($col); warn($row); MainLoop;
** if you use:
$graph_pane = $mw->Frame(
instead of:
$graph_pane = $mw->Scrolled('Frame', -scrollbars => 'osoe',
problem is solved! - but no scrollbar...

Thanks!

Replies are listed 'Best First'.
Re: Tk: Does the 'grid' geometry manager has a problem with a scrolled parent?
by liverpole (Monsignor) on Aug 07, 2006 at 14:59 UTC
    Hi tcarmeli,

    Try this:

    my ($col, $row) = $graph_pane->{'SubWidget'}->{'scrolled'}->gridSize( +);

    I think that will give you the answer you're looking for (4 x 3).

    You may also want to consider using strict and warnings in your code, which only requires adding 6 my declarations:

    #!/usr/bin/perl -w use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; my $graph_pane = $mw->Scrolled('Frame', -scrollbars => 'osoe', )->pack( -fill => 'both', -expand => 1, ); for my $i (0 .. 2){ for my $j (0 .. 3){ my $c=$graph_pane->Canvas( -width => 250, -height => 100, -background => 'white', )->grid( -row => $i, -sticky => "nsew", -column => $j, ); $c->createText(10,10, -text => $i . ',' . $j, -font => "{Arial} 8 bold", ); } } my ($col, $row) = $graph_pane->{'SubWidget'}->{'scrolled'}->gridSize( +); warn($col); warn($row); MainLoop;

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thanks!
      It works!
      Regarding the missing 'my', I just wrote a short example, it is not my 'real' code...Thanks!
      what does the following means:
      my ($col, $row) =$graph_pane->{'SubWidget'}->{'scrolled'}->gridSize();
      I had the error message:
      Can't call method "gridSize" on an undefined value at tes7 line 23.
        Hi tanyeun,

        It's difficult to know what's causing the error without seeing a bit more information.

        Are you sure you've created graph_pane correctly?

        Try putting this near the beginning of your code:

        use Data::Dumper; $Data::Dumper::Indent = 1;

        and just before the line where the error occurs (Can't call method "gridSize" on an undefined value at tes7 line 23) put this:

        printf "graph_pane => %s\n", Dumper(\$graph_pane);

        Assuming that graph_pane was assigned correctly, it should display the entire underlying data structure.  For example, I get:

        graph_pane => $VAR1 = \bless( { 'SubWidget' => { 'scrolled' => bless( { '_After_' => { 'after#3' => bless( [ ${$VAR1}->{'SubWidget'}->{'scrolled'}, 'after#3', 'idle', 'once', bless( [ 'Manage', ${$VAR1}->{'SubWidget'}->{'scrolled'}, bless( { # # ...Many lines omitted... # }, 'Tk::Frame' );

        Did you get something like the above, or something completely different?


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Tk: Does the 'grid' geometry manager has a problem with a scrolled parent?
by zentara (Cardinal) on Aug 07, 2006 at 14:49 UTC
    It gets messy when you mix grid with pack, I usually just stick with pack. Anyways, when you just use
    $graph_pane = $mw->Frame(
    the frame expands to encompass everything it contains.

    When you use the Scrolled('Frame it sets it's scrollbars to a small size because nothing is in it yet. If you tell it to use a specific size, is one workaround. Like:

    $mw->geometry('1000x200'); MainLoop;

    However, I question why you want to use 6 Canvas objects, when using one would be more efficient. Additionally, if you use one scrolled Canvas, you can use the -scrollregion along with canvas height and width to adjust what you see and can scroll.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hi,
      Thanks for the tips.
      This was just an example. In my application each canvas is some graph, scrollable on it own. The main reason I wanted to get the rows and columns in the first place was to set the size of the window since the number of rows and columns depends on the user's choices.
      Cheers
        I would make a main Scrolled Pane. Then in that Scrolled Pane, place your Scrolled Canvases in a series of stacked frames. You can use packForget to repack them according to user choice.
        #!/usr/bin/perl use strict; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry('400x250'); my $mwf = $mw->Scrolled('Pane', -scrollbars=>'osoe', -sticky=>'nwse', )->pack(-expand=>1, -fill=>'both'); my $f1 = $mwf->Frame()->pack(); my $f2 = $mwf->Frame()->pack(); my %canv; for (0..4){ $canv{$_}{'obj'} = $f1->Scrolled('Canvas', -height => 100, -width => 100, -bg =>'white', -scrollbars => 'osoe', -scrollregion => [ 0, 0, 500, 500 ], )->pack(-side =>'left' ,-padx=>10,-pady=>10); $canv{$_}{'obj'}->createText(50,50, -text => $_, -anchor => 'center', ); } for (5..9){ $canv{$_}{'obj'} = $f2->Scrolled('Canvas', -height => 100, -width => 100, -bg =>'white', -scrollbars => 'osoe', -scrollregion => [ 0, 0, 500, 500 ], )->pack(-side =>'left', -padx=>10,-pady=>10 ); $canv{$_}{'obj'}->createText(50,50, -text => $_, -anchor => 'center', ); } MainLoop();

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum