You are on the right track in your thinking, but the thing to remember is that Canvas items are NOT objects, and don't respond correctly to create/destroy ...... a Canvas is an object, but things you put into a canvas's surface are called Canvas Items. Items can be shown/hidden/deleted. Most times on a Canvas, if you wanted to reuse canvas items, you hide them, then reconfigure them in the background, then show them again. If you try to put another Canvas on top of another Canvas, it will probably not work as you expect, unless you put the other canvas into a createWindow of the underlying canvas.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
# the -stipple=>'transparent' option will still
# allow the bindings to work, but you can see the overlap
# See Chapter 17 of Mastering Perl/Tk
my $mw = MainWindow->new();
# first create a canvas widget
my $canvas = $mw->Canvas(width => 300, height => 200)->pack();
my $one = $canvas->createOval(55, 20, 200, 190,
-fill => 'blue',
-outline=>'blue',
-tags => ['blue'],
-stipple => 'transparent',
);
my $two = $canvas->createOval(105, 20, 250, 190,
-fill => 'red',
-outline=>'red',
-tags => ['red'],
-stipple => 'transparent',
);
my $ebutton = $mw->Button(-text => 'Exit',
-command => 'Tk::exit')->pack();
my $cbutton = $mw->Button(-text => 'Clear',
-command => sub{$canvas->delete('all')})->pack();
$canvas->Tk::bind("<Motion>", [ \&print_xy, Ev('x'), Ev('y') ]);
MainLoop();
sub print_xy {
my ($canv, $x, $y) = @_;
# print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n
+";
#trick to find overlapping objects, just use x1 = x and y1 = y
#to get a rectangular region of 1 point
my (@current) = $canvas->find('overlapping', $x, $y, $x, $y);
foreach my $id(@current){
print $canvas->gettags($id),' ';
}
print "\n";
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.