You are probably looking for the $canvas->bbox() method. It has some quirks(like outline pixels being off), but it is usable.
#!/usr/bin/perl
use Tk;
use strict;
my $x=10;
my $y=20;
my $x1=30;
my $y1=40;
my $mw=tkinit;
my $c=$mw->Canvas->pack;
my $rect = $c->createRectangle($x,$y,$x1,$y1,
-fill=>'red' );
print "rect-> $x $y $x1 $y1\n";
my ($bx,$by,$bx1,$by1)= $c->bbox($rect);
print "bbox-> $bx $by $bx1 $by1\n";
MainLoop;
| [reply] [d/l] |
I have tried that but I think that it does not give 'exact' results. I can read its desription
in Mastering Perl/Tk in such a way that this confirms my thought.
Therefore I was hoping that there would something like ->gridInfo (for widgets packed with grid) which would give the the data I am looking for (or even something like ->cget).I did try ->windowInfo but that gave an error.
Am I seeking for something that does not exist?
| [reply] |
I have an idea that bbox does not give ‘exact’ answers.
When widgets are packed with ‘grid’, ->gridInfo() gives you information including thepositional data I am looking for.
I have tried ->windowInfo() but that gave an error.
Is there an equivalent for items placed on a canvas (or am I looking for something that does not exist)?
PS I have now implemented move to hide widgets on a canvas – it does just what I wanted.
| [reply] |
Well there are ways of working around the inexact results from bbox, for instance, take the average of the x and y coords and that will give you the center of the item. You can work out a way with the anchor for the item, to get the exact position. You haven't shown any code and you are throwing around terms like "pack with grid" for canvas items. I'm not sure you understand...... the canvas widget itself can be packed or gridded, but items on the canvas cannot.....they are placed somewhere on the canvas. Unless you are talking about a grid item (read perldoc Tk::Canvas and search for createGrid). So you can't use packForget,etc, except on the canvas widget itself. As far as finding positions of items, each item type will have it's own quirks. Some items, you can get the "coords" or "x" ,"y". But you need to know how many points are there, like in polygons.
my @coords = $canvas->coords($id);
my ( $x0, $y0, $x1, $y1 ) = $canvas->coords($id);
Sometimes you need to use the bbox. The bbox is useful for finding a bounding box of a group of similarly tagged items. Bbox is also useful for text (to account for font size).It is not too hard to setup a hash, and store your positions exactly if that is easier for you. In Tk::Zinc( a suped-up canvas), positions can be kept with tget and tset (transformation matrix), see Newtons-Cradle-Tk-Zinc. In that case, it was difficult to get the balls to return to their starting rest position without a jitter effect.
| [reply] [d/l] |
One thing I forgot to mention, is that the Canvas is very efficient handling it's items. You usually can just delete and recreate them, without too much strain on the canvas memory. This is unlike Tk widgets
like the canvas, which should be reused to avoid memory gains.
| [reply] |