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

I have a Tk::NoteBook widget which I want to fill with graphs. Right now, I'd just like to place one graph per page. When I try to do this, the first page comes out fine, but on the subsequent pages, the graphs are squished up at the top. This code illustrates the problem. Is there a way around this? Thanks much.
use Tk; use Tk::Graph; use Tk::NoteBook; my $data = { Sleep => 51, Work => 135, Access => 124, mySQL => 5 }; my $to_register = { 'one' => [0,5,4,8,6,8], 'two' => [2,5,9,4,6,2], 'three' => [0,5,6,8,6,8], }; my $data1 = { 'one' => 3, 'two' => 3, 'three' => 3, }; $mw = MainWindow->new; my $nb = $mw->NoteBook(-ipadx => 10, -ipady => 10) ->pack(-side => 'top', -expand => '1', -fill => 'both'); ############# PAGE 1 ############# my $page1 = $nb->add("page1", -label => " test "); my $graph = $page1->Graph(-type => 'BARS')->pack(-expand => 1, -fill => 'both'); $graph->configure(-variable => $data); $graph->set($data); ############# PAGE 1 ############# my $page2 = $nb->add("page2", -label => " test1 "); my $graph1 = $page2->Graph(-type => 'Line', -max => 10, -look => 10)->pack(-expand => 1, -fill => 'both'); $graph1->register($to_register); $graph1->variable($data1); MainLoop;

Replies are listed 'Best First'.
Re: Multiple Tk::Graphs with Tk::Notebook widget
by zentara (Cardinal) on May 02, 2005 at 12:14 UTC
    Hi, sometimes with notebook, you have to use the -raisecmd option for the tabs. Here is a working version of your script. I just minimally hacked it to make it work, but it demonstrates the principle. You probably need to redesign the "add tabs" part , and put it in a loop, storing your data and objects in a hash. Anyways, this works, but needs improving. (I know this is'nt obvious, I happened to know it by previous hacking with notebook)
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Graph; use Tk::NoteBook; my $data = { Sleep => 51, Work => 135, Access => 124, mySQL => 5 }; my $to_register = { 'one' => [ 0, 5, 4, 8, 6, 8 ], 'two' => [ 2, 5, 9, 4, 6, 2 ], 'three' => [ 0, 5, 6, 8, 6, 8 ], }; my $data1 = { 'one' => 3, 'two' => 3, 'three' => 3, }; my $mw = MainWindow->new; my $nb = $mw->NoteBook( -ipadx => 10, -ipady => 10, )->pack( -side => 'top', -expand => '1', -fill => 'both' ); ############# PAGE 1 ############# my $page1 = $nb->add( "page1", -label => " Page1 ", -raisecmd => sub{ $mw->update; }, ); my $graph = $page1->Graph( -type => 'BARS' )->pack( -expand => 1, -fill => 'both' ); $graph->configure( -variable => $data ); $graph->set($data); ############# PAGE 2 ############# my $graph1; my $graph1_set = 0; my $page2; $page2 = $nb->add( "page2", -label => " Page2 ", -raisecmd => sub{ return if $graph1_set; $graph1 = $page2->Graph( -type => 'Line', -max => 10, -look => 10, )->pack( -expand => 1, -fill => 'both' ); $graph1->register($to_register); $graph1->variable($data1); $mw->update; $graph1_set = 1; }, ); MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      You solution makes good sense (and works great). I guess the -raisecmd option allows you to redraw the graph when the tab is clicked. Without it, I assume all the graphs will be drawn only once at runtime. Another lesson learned. Many thanks.
Re: Multiple Tk::Graphs with Tk::Notebook widget
by PodMaster (Abbot) on May 02, 2005 at 05:11 UTC
    So I `perldoc Tk::Graph' and I see
     -debug [*0*|1\
       This is the switch for debug output at the normal console (STDOUT)
    , so I turn -debug on for my $graph1 = $page2->Graph( and I see
    Width: 1, Height: 1
    
    That ought to explain something

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Hmm... I also tried activating debugging on the first graph. Bunch of output, as apposed to the one line from the second graph. Maybe you can only do one graph per NoteBook? I'm stabbing in the dark.

      By trial and error, I've concluded that when using a notebook, the graph must be on the first page. You get the 1x1 if it is on any other page accept the first.

        Maybe you can only do one graph per NoteBook?..
        No. You can, but it's currently buggy. Tk::Notebook (or something) for reporting width/height 1, and Tk::Graph for not allowing you to override width/height. If I were you I'd go sourcediving and try to patch and/or contact the authors/perltk-list for help.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Multiple Tk::Graphs with Tk::Notebook widget
by Courage (Parson) on May 02, 2005 at 09:56 UTC