I have aquired the book "Mastering Perl -Tk" and started to learning tk, by small examples.
Now there are some mysteries that I have no explanation for, maybee you could help me?

Tk Mystery 1.
I have a canvas which is 500 x 500. The visible region is 200 x 200.
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; my $c = $mw->Scrolled('Canvas', -width => 200, -height => 200, -background =>'blue', -scrollregion => [ 0, 0, 500, 500 ] ); $c->pack(-expand => -2, -fill => 'both'); $c->createRectangle(100, 100, 150, 150, -fill => 'yellow'); my $plus = $c->Button( -text => ' + ', -command => sub {$c->scale("all", 0, 0, 2, 2); } ); $plus->pack; MainLoop;
If the button just before the main loop on the canvas, is deleted this causes the canvas to appear in the normal size 200 x 200. However if the button is present the canvas will not have the normal dimensions 200 x 200, but will be microsized and centered around the button, could you explain why this is so?

Tk Mystery 2.
I have another program including a scrollable canvas, on which it is possible to draw rectangles. The program was orginaly written by mr. Liverpole, and has been modified slightly by me. When rightclicking on the canvas and drawing a rectangle the coordinates of the mouse pointer on the canvas are written in the command window. The intressting thing is that if the canvas is scrolled, coordinates at the canvas seem to move together with the scroll bar. For example the coordinate 0,0 will be at the upper left corner of the visual canvas part regardless if the canvas have been scrolled at any direction or not. The rectangles drawn will appear in wrong possitions if the canvas is scrolled. It seems therefore that the canvas registers the change in coordinates by scrolling, while the mouse pointer doesn´t do this. Could you please give me an explanation why the program seem to disregard the change of coordinates that one could suppose would happen by scrolling, and why the recangles are drawn in wrong places when the canvas is scolled?
#!/usr/bin/perl -w use Tk; use strict; # Globals my $b_button_1_down = 0; # Is the left mouse button currently down? my ($x0, $y0, $x1, $y1); # Starting and ending (x,y) coordinates my $rect_id = 0; # Last rectangle drawn # Main program / GUI setup my $mw = MainWindow->new; my $c = $mw->Scrolled('Canvas', -width => 200, -height => 200, -background =>'blue', -scrollregion => [ 0, 0, 1500, 1000 ] ); $c->pack(-expand => 1, -fill => 'both'); bind_left_mouse($c); MainLoop; # Subroutines sub bind_left_mouse { my ($c) = @_; # Create "callback"; subroutines which get called whenever the # corresponding event is triggered. Note that Ev('x') and Ev('y') # will tell us, at the time they're used, what the current (x,y) # coordinate pair was. # my $cb1 = [ \&left_mouse_down, $c, Ev('x'), Ev('y')]; my $cb2 = [ \&left_mouse_release, $c, Ev('x'), Ev('y')]; my $cb3 = [ \&left_mouse_moving, $c, Ev('x'), Ev('y')]; # Bind the callbacks $c->CanvasBind("<ButtonPress-1>", $cb1); $c->CanvasBind("<ButtonRelease-1>", $cb2); $c->CanvasBind("<Motion>", $cb3); } sub left_mouse_down { # This gets called whenever the left-mouse button is clicked my ($ev, $c, $x, $y) = @_; $b_button_1_down = 1; ($x0, $y0) = ($x, $y); print "(debug) button1 down, (x,y) => ($x,$y)\n"; # ($first, $last) = $c->get( ); #print "F $first L $last\n"; } sub left_mouse_moving { # This gets called whenever the mouse moves. It's true for ALL # mouse motion, but we're really just interested in when the mouse # is moving -and- the left mouse button is held down; hence the na +me. # my ($ev, $c, $x, $y) = @_; return unless $b_button_1_down; print "(debug) button1 moving, (x,y) => ($x,$y)\n"; $rect_id and $c->delete($rect_id); # Delete any old rectangle firs +t $rect_id = $c->createRectangle($x0, $y0, $x, $y, -outline => 'yell +ow', -dash => '. '); } sub left_mouse_release { # This gets called whenever the left-mouse button is released my ($ev, $c, $x, $y) = @_; $b_button_1_down = 0; ($x1, $y1) = ($x, $y); print "(debug) button1 release, (x,y) => ($x,$y)\n"; # Now do something with the triangle at (x0,y0,x1,y1)... $rect_id and $c->delete($rect_id); # Delete any old rectangle firs +t $rect_id = $c->createRectangle($x0, $y0, $x, $y, -outline => 'yell +ow', -dash => '. '); }

In reply to Tk mysteries. by tamaguchi

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.