Hi,
Please have a look on the folowing code, it is based on some code I was kindly offered by Rob (user: rcseege) for a previous post of mine.
The question is:
How can I change the 'AlignAll' subroutine so it will move the scrollbars of all 4 canvases so I will get all 4 rectangles aligned in the middle of the viewable area?
Few notes:
1. the user can first zoom in/out and than hit the 'Align' button.
2. You can assume the user hasn't zooomed out too much (in which case it might be impossible to align the rectangles).
3. I tried playing with the scanMark and scanDragto method but could not get it to work. Any explanation on these is more than welcome (the basic doc. on that left me with some big ???...).
Thanks!
use strict; use Tk; use Tk::Pane; my @tile; my @sc; my $x; my $i; my $initial_width=800; my $scale=1; my $mw = MainWindow->new; my $frame = $mw->Scrolled("Frame", -scrollbars => 's', -sticky => 'nsew', -bg => 'black', -width => 450, -height => 650 )->pack(qw/-expand 1 -fill both/); foreach $i (0 .. 3) { $tile[$i] = $frame->Frame( -bg => 'black' )->pack(); $sc[$i] = $tile[$i]->Scrolled('Canvas', -scrollbars => 'os', -scrollregion => [0, 0, $initial_width, 125], -width => 400, -height => 125, -background => randColor() )->pack(qw/-fill both -side top -expand 1/); $x = randNumber(); $sc[$i]->createRectangle($x, 50, $x+20, 90, -fill => randColor(), -outline => 'black', -width => 2, -tags => 'rect', ); $sc[$i]->createLine( 10,100, 790,100, -arrow => 'last'); for($x=50;$x<800;$x+=50){ $sc[$i]->createLine( $x,95, $x,105 ); } } $frame->Button( -text => " Zoom Out ", -width => 10, -command => sub { $scale*=0.8; foreach $i (0 .. 3) { $sc[$i]->scale(qw/all 0 0 .8 1/); $sc[$i]->configure(-scrollregion => [0, 0, int +($scale*$initial_width), 125]); } } )->pack(qw/-side left -padx 25/); $frame->Button( -text => " Zoom In ", -width => 10, -command => sub { $scale*=1.25; foreach $i (0 .. 3) { $sc[$i]->scale(qw/all 0 0 1.25 1/); $sc[$i]->configure(-scrollregion => [0, 0, int +($scale*$initial_width), 125]); } } )->pack(qw/-side left -padx 25/); $frame->Button( -text => " Align ", -width => 10, -command => [\&AlignAll], )->pack(qw/-side left -padx 25/); MainLoop; sub AlignAll{ my ($x1, $y1, $x2, $y2); my $i; my $location; foreach $i (0 .. 3) { ($x1, $y1, $x2, $y2) = $sc[$i]->bbox('rect'); $location =int(($x1+$x2)/2); warn("$location"); } } sub randColor { my @colors = qw(red yellow blue orange green purple cyan pink); return $colors[rand($#colors + 1)]; } sub randNumber { my ($max, $min) = (600, 200); my $size = int(rand($max)); $size += $min if $size < $min; return $size; }

In reply to Tk: Aligning the items in seperate canvases by tcarmeli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.