in reply to Re: Unbind canvas items and Forget
in thread Unbind canvas items and Forget
Thanks for the replies! I put together a quick example. Keep in mind this is a very rough sketch...
Basically what it does, is set up the frame/canvas structure, then invokes &scan1start; which is a subroutine to build the first fake canvas button, and bind it to right and left mouse (not the reset button, leave that alone for now!)
Clicking the button runs the next two subroutines to create two more buttons (and also changes the first button's color). I have some commented code as in my working example this changes an image not the background color, but you get the idea.
Clicking either of these new buttons will change their color, and right-clicking will change it back. Right-clicking the first button hides the two higher buttons, but only if they are "off" (black in this example.)
You can test all of this and it works fine, until you click the reset button. This button is supposed to basically destroy all the buttons, then run the initial subroutine to re-create the first button and set up it's bindings, basically starting all over. However, as you can see perl complains a ton and the button is never re-created.
Here's the sample code:
#!/usr/bin/perl use warnings; use Tk; use strict; my $mw = new MainWindow; $mw-> geometry('640x480'); $mw-> resizable( 0, 0 ); my $numscan1 = 0; my $numscan2 = 0; my $numscan3 = 0; my $leftframe = $mw-> Frame(); $leftframe->form(-left => '%0', -right => '%25', -bottom => '%99', -to +p => '%1'); my $resetb = $leftframe-> Button(-text => 'Reset', -command => \&reset + )->form(-left => '%50', -top => '%50'); my $rightframe = $mw-> Frame(-relief => 'groove', -borderwidth => 1); $rightframe-> form(-left => $leftframe, -right => '%99', -bottom => '% +99', -top => '%1'); my $maincanvas = $rightframe-> Canvas(-background => 'blue', -highligh +tthickness => 0); $maincanvas-> form(-left => '%1', -right => '%99', -bottom => '%99', - +top => '%1'); # I use canvas so I can have a background image beneath my items. # This is how I was doing that, though for this example # I'm just using background color instead: #my $maincanvasi = $mw-> Photo(-file => "background.jpg"); #$maincanvas->createImage(0,0, -image => $maincanvasi, -anchor => 'nw' +); #Here are some fake buttons using sub-canvas: my $scan1 = $maincanvas-> Canvas(-width => 64, -height => 54, -highlig +htthickness => 0); #my $scan1img1 = $maincanvas->Photo(-file => "scan1img1.jpg"); #my $scan1img2 = $maincanvas->Photo(-file => "scan1img2.jpg"); my $scan2 = $maincanvas-> Canvas(-width => 64, -height => 54, -highlig +htthickness => 0); #my $scan2img1 = $maincanvas->Photo(-file => "scan2img1.jpg"); #my $scan2img2 = $maincanvas->Photo(-file => "scan2img2.jpg"); my $scan3 = $maincanvas-> Canvas(-width => 64, -height => 54, -highlig +htthickness => 0); #my $scan3img1 = $maincanvas->Photo(-file => "scan3img1.jpg"); #my $scan3img2 = $maincanvas->Photo(-file => "scan3img2.jpg"); &scan1start; MainLoop; sub reset { my @buttons = ($scan1,$scan2,$scan3); foreach (@buttons) { $_-> destroy; } &scan1start; } sub scan1start { if ($numscan1 == 0) { $scan1-> form(-left => '187', -top => '319'); #$scan1-> createImage(0, 0, -image => $scan1img1, -anchor => ' +nw'); $scan1-> configure(-background => 'black'); $scan1-> Tk::bind('<Button-1>' => sub { #$scan1->delete($scan1img1); $numscan1 = 1; &scan2start; &scan3start; &scan1start; } ); $scan1-> Tk::bind('<Button-3>' => sub { }); } elsif ($numscan1 == 1) { $scan1-> form(-left => '187', -top => '319'); #$scan1-> createImage(0, 0, -image => $scan1img2, -anchor => ' +nw'); $scan1-> configure(-background => 'white'); $scan1-> Tk::bind('<Button-3>' => sub { if (($numscan2 == 0) && ($numscan3 == 0)) { #$scan2->delete($scan2img1); #$scan3->delete($scan3img1); #$scan1->delete($scan1img2); $scan2->formForget; $scan3->formForget; $numscan1 = 0; &scan1start; } } ); $scan1-> Tk::bind('<Button-1>' => sub { }); } } sub scan2start { if ($numscan2 == 0) { $scan2-> form(-left => '102', -top => '211'); #$scan2-> createImage(0, 0, -image => $scan2img1, -anchor => ' +nw'); $scan2-> configure(-background => 'black'); $scan2-> Tk::bind('<Button-1>' => sub { #$scan2->delete($scan2img1); $numscan2 = 1; &scan2start; } ); $scan2-> Tk::bind('<Button-3>' => sub { }); } elsif ($numscan2 == 1) { $scan2-> form(-left => '102', -top => '211'); #$scan2-> createImage(0, 0, -image => $scan2img2, -anchor => ' +nw'); $scan2-> configure(-background => 'white'); $scan2-> Tk::bind('<Button-3>' => sub { #$scan2->delete($scan2img2); $numscan2 = 0; &scan2start; } ); $scan2-> Tk::bind('<Button-1>' => sub { }); } } sub scan3start { if ($numscan3 == 0) { $scan3-> form(-left => '202', -top => '211'); #$scan3-> createImage(0, 0, -image => $scan3img1, -anchor => ' +nw'); $scan3-> configure(-background => 'black'); $scan3-> Tk::bind('<Button-1>' => sub { #$scan3->delete($scan3img1); $numscan3 = 1; &scan3start; } ); $scan3-> Tk::bind('<Button-3>' => sub { }); } elsif ($numscan3 == 1) { $scan3-> form(-left => '202', -top => '211'); #$scan3-> createImage(0, 0, -image => $scan3img2, -anchor => ' +nw'); $scan3-> configure(-background => 'white'); $scan3-> Tk::bind('<Button-3>' => sub { #$scan3->delete($scan3img2); $numscan3 = 0; &scan3start; } ); $scan3-> Tk::bind('<Button-1>' => sub { }); } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Unbind canvas items and Forget
by zentara (Archbishop) on Nov 01, 2012 at 10:49 UTC | |
by Phinix (Acolyte) on Nov 03, 2012 at 01:18 UTC | |
by zentara (Archbishop) on Nov 03, 2012 at 09:05 UTC |