binding an event to an item in a canvas widget 
Author Message
 binding an event to an item in a canvas widget

I have some rectangles in a canvas widget and I'm trying to bind an
event to them but it doesn't seem to take. I was wondering if anyone can
tell me why the following code doesn't work.  Thanks, James

#!/usr/bin/perl
use Tk;

$mw=MainWindow->new();
$fwCanvas = $mw->Frame( )->pack;

$gcwCanvas = $fwCanvas->Scrolled('Canvas',
                     -width=> 800,
                     -height => 500,
                     -scrollregion => [0,0,2000,2000])->pack();
for ($i = 1; $i < 10; $i++) {
    $name = "X".$i;
    $x = $i*100;

    $gcwCanvas->createRectangle($x,
                    10,
                    $x+90,
                    150,
                       -tag => $name,
                    -width=> 2);
    $gcwCanvas->createText($i*100+45,
                       80,
                       -text => $name);

    $gcwCanvas->Tk::bind($name,
                "<Enter>",
                 sub { print "hi!\n" });

Quote:
}

MainLoop();


Mon, 10 Dec 2001 03:00:00 GMT  
 binding an event to an item in a canvas widget

Quote:

> I have some rectangles in a canvas widget and I'm trying to bind an
> event to them but it doesn't seem to take. I was wondering if anyone can
> tell me why the following code doesn't work.  Thanks, James

Use $canvas->bind for canvas items and $canvas->Tk::bind for the whole
canvas.

Regards,
        Slaven

--
use Tk;$c=tkinit->Canvas->pack;$x=5;for(split/_/,'KPI1_+09IPK_K;-OA1_+K!;A__1;
Q!7G_1+QK_3CLPI90,_+K!;A_+1!KQ!.N_K+1Q!.F_1+KN.Q__1+KN._K+1Q!.F_1+KN.Q_+1Q__+1!
KQ!.N_1;Q!7G_K3,09Q_+1!K.Q_K+1Q!.F_1+KN.Q'){s/\n//g;for(split/!/){$c->create(
'line',map{$a=-43+ord;($x+($a>>3)*2,5+($a&7)*2)}split//)}$x+=12}MainLoop



Mon, 10 Dec 2001 03:00:00 GMT  
 binding an event to an item in a canvas widget
James,

don't say    $gcwCanvas->Tk::bind($name, ...
say          $gcwCanvas->bind($name,
instead.

Note that "<Enter>" invokes the callback whenever you enter the line, not the
rectangle (say it invokes the callback when you enter the rectangle _and_ when
you leave the rectangle.

Note also that when the lines are very thin Tk will not always detect that
you've crossed them with the mouse.

Regards,

Juergen

----------------------------------------------------------

Lee & James schrieb:

Quote:

> I have some rectangles in a canvas widget and I'm trying to bind an
> event to them but it doesn't seem to take. I was wondering if anyone can
> tell me why the following code doesn't work.  Thanks, James

> #!/usr/bin/perl
> use Tk;

> $mw=MainWindow->new();
> $fwCanvas = $mw->Frame( )->pack;

> $gcwCanvas = $fwCanvas->Scrolled('Canvas',
>                      -width=> 800,
>                      -height => 500,
>                      -scrollregion => [0,0,2000,2000])->pack();
> for ($i = 1; $i < 10; $i++) {
>     $name = "X".$i;
>     $x = $i*100;

>     $gcwCanvas->createRectangle($x,
>                     10,
>                     $x+90,
>                     150,
>                        -tag => $name,
>                     -width=> 2);
>     $gcwCanvas->createText($i*100+45,
>                        80,
>                        -text => $name);

>     $gcwCanvas->Tk::bind($name,
>                 "<Enter>",
>                  sub { print "hi!\n" });
> }
> MainLoop();



Tue, 11 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Key events and canvas items.

2. Keyboard events and canvas items

3. Problem with binding events on TkPerl canvas

4. Binding mouse event to a canvas

5. Strange Canvas Rectangle Item Bind Behavior

6. Binding to items on Scrolled Canvas

7. Binding Canvas Items question

8. help binding mouse events to canvas

9. Canvas Item Bindings

10. how to delete items from a canvas widget?

11. CANVAS-widget: slow in displaying many items

12. detecting mouse events with canvas widget

 

 
Powered by phpBB® Forum Software