Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

perl tk graphviz

by dlal66 (Acolyte)
on Feb 23, 2012 at 14:33 UTC ( [id://955742]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I have a perl/tk application that uses graphviz to plot a graph.
my $gv = $root->Scrolled('GraphViz', -background => '#B0C0FF', -scrollbars=>'se', ) ->pack (-side=>'top',-expand =>1, -fill=>'both');
All is well except that what I'd like is that when a user resizes the window, the graph displayed should be "layedout" again so that it adjusts to the size of the window. I first tried to bind the '<Configure>' event to the canvas this way:
my $canvas = $gv->Subwidget("canvas'); $canvas->bind('<Configure>', sub { my $w = shift; print $w->Width . " and " . $w->Heig +ht . "\n"; &redo_layout(); };
But nothing happens! I the binding is not used and the Subwidget returns an undefined "$canvas". So I modified the above code to bind the root window with Configure as in:
$root->bind('<Configure>', sub { my $w = shift; print $w->Width . " and " . $w->Heig +ht . "\n"; &redo_layout(); };
This works! But the behavior is not great. The graph layout does adjust but is a very "flickery" way. And strangely, window resize prints a lot of values for width and height when the bound subroutine is called. What can I do to make this a nice user experience? Why can't I bind '<Configure>' to the subwidget "canvas'? Thank you in advance.

Replies are listed 'Best First'.
Re: perl tk graphviz
by zentara (Archbishop) on Feb 23, 2012 at 15:58 UTC
    I'm not sure what your problem is, but the "flicker" may be caused by Graphviz trying to rebuild it's graphics and display it at each Configure event. As you already see, the Configure event fires many times while resizing. You may be able to work out a flag, where a flag is set when the mouse button 1 is pressed, and set back to zero when the button 1 is released. Then in your Configure binding, you can return if the flag is set, thus only having Graphviz rebuild at the end of the resize. See Re: perltk autoresize following users resize for a similar hack using Enter and Leave bindings.

    You might also look at the Tk::Panedwindow widget. It's perldoc says

    RESIZING PANES A pane is resized by grabbing the sash (or sash handle if present) and + dragging with the mouse. This is accomplished via mouse motion bindings on the + widget. When a sash is moved, the sizes of the panes on each side o +f the sash, and thus the widgets in thosepanes, are adjusted. When a pane is resized from outside (eg, it is packed to expand and fi +ll, and the containing toplevel is resized), space is added to the fi +nal (rightmost or bottommost) pane in the window.
    An example:
    #!/usr/bin/perl -w use strict; use Tk; my @scrollregion = (0,0,500,500); my $mw=tkinit; my $windowpane = $mw->Panedwindow( -orient => 'vertical' ) ->pack( -side => 'top' , -expand => 1, -fill => 'both' ); my $firstframe=$mw->Frame(); my $secondframe=$mw->Frame(); my $cf=$firstframe->Scrolled( 'Canvas', -bg=>'red', -scrollregion=>\@scrollregion, -confine=>1, -scrollbars=>'se') ->pack(-expand=>1, -fill=>'both', -anchor=>'nw'); $cf->create('rectangle',0,0,500,500); my $statusbar = $firstframe->Label( -text=>"This is a statusbar") ->pack(-fill=>'x', -expand=>0); my $tw = $secondframe->Scrolled( 'Text', -scrollbars=>'osoe') ->pack(-fill=>'both', -expand=>1, -anchor=>'nw'); $windowpane->add($firstframe, -sticky=>'nsew'); $windowpane->add($secondframe, -sticky=>'nsew'); $windowpane->bind( '<Configure>' => \&OnResize ); $mw->bind( '<Configure>' => \&OnResize ); MainLoop; sub OnResize{ my ($newx,$newy) = ($mw->width, $mw->height); $cf->configure( -scrollregion=>[0,0,$newx,$newy]); } #########################################################

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you. I tried using the <Leave> and <Enter> bindings but it did not really help in the sense that the resize did not always happen for some reason. BUT I was able to resolve the issue by using CanvasBind instead of Bind. I therefore now have something like:
      $gv->CanvasBind('<Configure>',...)
      and that does the job perfectly (no flickering, etc.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-03-28 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found