Binding Canvas Items question 
Author Message
 Binding Canvas Items question

I'm new to PerlTk and am attempting to bind <Enter> or <Button-1> events to
Polygons I've created within a canvas widget.

From what I've read in the Oreilly book and syntax errors I don't think this
can be done directly via $leftrect->Tk::bind("<Button-1>",...

I've been able to bind by tag without syntax error unfortunately nothing
happens.  $c->Tk::bind("right", "<Enter>",
        sub { $c->itemconfigure("right", -fill =>"red") });

I'm a reasonable programmer so if I can get working source code on the issue
I'll be able to figure it out, but I'll also happily accept an explaination.

Thank you

greg

Sent via Deja.com
http://www.*-*-*.com/



Wed, 25 Jun 2003 00:52:31 GMT  
 Binding Canvas Items question

Quote:

> I'm new to PerlTk and am attempting to bind <Enter> or <Button-1>
events to
> Polygons I've created within a canvas widget.

> From what I've read in the Oreilly book and syntax errors I don't
think this
> can be done directly via $leftrect->Tk::bind("<Button-1>",...

> I've been able to bind by tag without syntax error unfortunately
nothing
> happens.  $c->Tk::bind("right", "<Enter>",
>    sub { $c->itemconfigure("right", -fill =>"red") });

This is because you are using the wrong 'bind' method. Tk::bind is for
the canvas widget itself not the items within the canvas. This is the
same as the canvasbind method.

You must specify some sort of tag or ID for any items you wish to bind.
This could even be all the items if you wish...using 'all'. The
Tk::Canvas and Tk::bind documentation has all this info.

Here is an example showing the differences: (Here I just use the tag,
however, you could use ID's as well)
############################################
use Tk;
use strict;


my $status;

my $mw = MainWindow->new;
my $c = $mw->Canvas(-width=>400,-height=>400)->pack;
my $l = $mw->Label(-textvariable=>\$status)->pack;


$c->Tk::bind('<Enter>',sub {$status="You have entered the CANVAS!";});
$c->Tk::bind('<Leave>',sub {$status="Your mouse has left the
Canvas!";});
$c->bind($poly1,'<Enter>',sub {$c->itemconfigure($poly1,-fill=>'red');
        $status="Changed it to red!";});
$c->bind($poly1,'<Leave>',sub {$c->itemconfigure($poly1,-fill=>'blue');
        $status="Back to Blue!";});
$c->bind($poly1,'<Any-ButtonPress>',sub {$status="Nice click!"});

MainLoop;

sub random_poly
{

        foreach(0..7){

        }

Quote:
}

############################################

Sent via Deja.com
http://www.deja.com/



Thu, 26 Jun 2003 17:34:53 GMT  
 Binding Canvas Items question

Quote:



> > I'm new to PerlTk and am attempting to bind <Enter> or <Button-1>
> events to
> > Polygons I've created within a canvas widget.

> > From what I've read in the Oreilly book and syntax errors I don't
> think this
> > can be done directly via $leftrect->Tk::bind("<Button-1>",...

> > I've been able to bind by tag without syntax error unfortunately
> nothing
> > happens.  $c->Tk::bind("right", "<Enter>",
> >       sub { $c->itemconfigure("right", -fill =>"red") });

> This is because you are using the wrong 'bind' method. Tk::bind is for
> the canvas widget itself not the items within the canvas. This is the
> same as the canvasbind method.

> You must specify some sort of tag or ID for any items you wish to
bind.
> This could even be all the items if you wish...using 'all'. The
> Tk::Canvas and Tk::bind documentation has all this info.

> Here is an example showing the differences: (Here I just use the tag,
> however, you could use ID's as well)

Oops! I meant I used the ID...and tags can be used as well.

- Show quoted text -

Quote:
> ############################################
> use Tk;
> use strict;


> my $status;

> my $mw = MainWindow->new;
> my $c = $mw->Canvas(-width=>400,-height=>400)->pack;
> my $l = $mw->Label(-textvariable=>\$status)->pack;



> $c->Tk::bind('<Enter>',sub {$status="You have entered the CANVAS!";});
> $c->Tk::bind('<Leave>',sub {$status="Your mouse has left the
> Canvas!";});
> $c->bind($poly1,'<Enter>',sub {$c->itemconfigure($poly1,-fill=>'red');
>    $status="Changed it to red!";});
> $c->bind($poly1,'<Leave>',sub

{$c->itemconfigure($poly1,-fill=>'blue');

- Show quoted text -

Quote:
>    $status="Back to Blue!";});
> $c->bind($poly1,'<Any-ButtonPress>',sub {$status="Nice click!"});

> MainLoop;

> sub random_poly
> {

>    foreach(0..7){

>    }

> }
> ############################################

> Sent via Deja.com
> http://www.deja.com/

Sent via Deja.com
http://www.deja.com/


Sat, 28 Jun 2003 04:10:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Strange Canvas Rectangle Item Bind Behavior

2. Binding to items on Scrolled Canvas

3. binding an event to an item in a canvas widget

4. Canvas Item Bindings

5. Canvas bindings - perf question

6. how to delete items from a canvas widget?

7. Moving canvas window items

8. Squezing items onto a Canvas without scrollbars

9. raising/lowering Canvas items

10. "Hiding" items on a canvas

11. Canvas item tagging

12. detect overlapping items in a canvas?

 

 
Powered by phpBB® Forum Software