Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

getting visible area of Tk::Scrolled?

by halley (Prior)
on Sep 16, 2011 at 18:39 UTC ( #926462=perlquestion: print w/ replies, xml ) Need Help??
halley has asked for the wisdom of the Perl Monks concerning the following question:

I am playing around some more with Tk::Canvas, and in particular, the scrolled variety. The scrollbar mechanisms seem really primitive.

my $c = $parent->Scrolled('Canvas', -width => $width, -height => $height, -background => 'beige', -scrollbars => 'osoe', -scrollregion => [ 0, 0, $width, $height ]);

I've constructed a Scrolled Canvas, which apparently returns a ref of type Tk::Frame. That must have the children, including a Canvas and a couple of Scrollbars.

I also found the Tk::Autoscroll class, which didn't come with my build of Perl/Tk, but was easy enough to add.

When dragging around an item in a large scrolled canvas, I want to auto-scroll to keep the dragged item in view. This doesn't seem so hard, except knowing where the scroller is viewing at the current time.

I can't figure out the way to go from a blessed Canvas ref (given in the drag events), to calculate or query the area of the scrollregion that is currently visible.

Before I go digging farther, I bet someone already has some code that calculates the visible area.

--
[ e d @ h a l l e y . c c ]

Comment on getting visible area of Tk::Scrolled?
Download Code
Re: getting visible area of Tk::Scrolled?
by BrowserUk (Pope) on Sep 16, 2011 at 19:05 UTC

    To find the canvas coordinate that is currently being displayed at the top left corner of the canvas window, use:

    my $canvasTLx = $canvas->canvasx( 0 ); my $canvasTLy = $canvas->canvasy( 0 );

    See also the -confine & -scrollregion options to the canvas widget.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Partly there. What are the screen X, Y of the lower right corner of the canvas window? Accurately taking into account whether scrollbars have appeared or not?

      --
      [ e d @ h a l l e y . c c ]

        Use $canvas->get_corners to obtain the bounding box of the visible portion of the canvas.

        See also the return lists from the ->xview() & ->yview() methods which are useful for manipulating the scrollbars.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: getting visible area of Tk::Scrolled?
by zentara (Archbishop) on Sep 16, 2011 at 20:21 UTC
    You may be looking for $canvas->bbox('all'), also see Two questions regarding -tk-canvas
    #!/usr/bin/perl use Tk; use Tk::Canvas; use Tk::Label; my $mw = tkinit(-title, 'Canvas'); $mw->geometry('250x500'); my $frame = $mw->Frame(-width, 250, -height, 250)->pack(-fill, 'both', + -expand,1); my $canvas = $frame->Scrolled('Canvas', -width, 300, -height, 300, -bg=>'white', -scrollbars, 'osoe' )->pack(-fill,'both',-expand,1); my $btn = $mw->Button(-text, "Add Stuff", -command,\&addStuff) ->pack(); my $btn1 = $mw->Button(-text, "Add More Stuff", -command,\&addmoreStuf +f) ->pack(); MainLoop; sub addStuff{ $canvas->createText(500, 500, -text, "Some text @ 500, 500." ); $canvas->createRectangle( 50, 50, 110, 110, -fill, 'red'); $canvas->createRectangle( 150, 150, 210, 210, -fill, 'green'); $canvas->createRectangle( 250, 250, 310, 310, -fill, 'blue'); $canvas->createText(20, 20, -text, "Some text @ 20, 20." ); # # Your can use an array for your bounding box # commented out to show both ways to set the scrollregion # Just make sure you are 1 less on lower right and bottom # Or you will have out of bounds array... easy to forget # if you don't use a frame with your canvas $canvas->configure(-scrollregion => [ 0, 0, 599, 599]); # $canvas->configure(-scrollregion => [$canvas->bbox('all')]); } sub addmoreStuff{ $canvas->createText(1500, 1500, -text, "Some text @ 1500, 1500." ); $canvas->createRectangle( 5000, 5000, 1100, 1100, -fill, 'red'); $canvas->createRectangle( 1500, 1500, 2100, 2100, -fill, 'green'); $canvas->createRectangle( 2500, 2500, 3100, 3100, -fill, 'blue'); $canvas->createText(2000, 10, -text, "Some text @ 2000, 10." ); # # Your can use an array for your bounding box # commented out to show both ways to set the scrollregion # Just make sure you are 1 less on lower right and bottom # Or you will have out of bounds array... easy to forget # if you don't use a frame with your canvas #$canvas->configure(-scrollregion => [ 0, 0, 599, 599]); $canvas->configure(-scrollregion => [$canvas->bbox('all')]); }

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-07-30 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls