Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^4: Unbind canvas items and Forget

by Phinix (Acolyte)
on Nov 03, 2012 at 01:18 UTC ( #1002066=note: print w/replies, xml ) Need Help??

in reply to Re^3: Unbind canvas items and Forget
in thread Unbind canvas items and Forget

I see you are using raise and lower to show/hide items rather than destroying/re-creating them. That could work, though it would require some re-coding.

I have tried using frames rather than sub-canvas, as well as other widgets, but run into the same error when attempting to destroy and re-create bound widgets. It would be really nice to know what internal reference is being maintained to these events and stop processing on them so I could destroy and rebuild, simulating re-starting the application from scratch when their structure is initially created...

For now the raise/lower method is an effective enough workaround, so I thank you. I would still like to discover the way to do as I intended though, eventually, just for my own edification.

Replies are listed 'Best First'.
Re^5: Unbind canvas items and Forget
by zentara (Archbishop) on Nov 03, 2012 at 09:05 UTC
    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"; }

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002066]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-05-27 02:22 GMT
Find Nodes?
    Voting Booth?