Home > Archive > PerlTk > September 2005 > Bind Mouse event to canvas background?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Bind Mouse event to canvas background?
|
|
| Josef Moellers 2005-09-29, 7:56 am |
| Hi,
I'd like to bind a mouse button press to a canvas.
I tried
#! /usr/bin/perl
use warnings;
use strict;
use Tk;
sub MeldeDich {
print STDERR join(" ", @_), "\n";
}
# Create main window.
my $main =3D new MainWindow;
my $canvas =3D $main->Canvas(
-height =3D> "12c",
-width =3D> "16c",
-background =3D> "blue",
);
$canvas->pack(-side=3D>"top", -fill=3D>"x");
$canvas->bind($canvas, '<Button-1>', [ \&MedleDich, "Hello\n" ]);
$canvas->bind($canvas, '<Button-1>', [ \&MedleDich, "Hello\n" ]);
MainLoop;
but I get no output ...
In the end, I'd like to know where on the canvas I have pressed the=20
mouse button (I'd like to select a region of a graph for zooming).
Josef
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
| |
|
|
"Josef Moellers" <josef.moellers@fujitsu-siemens.com> wrote in message
news:dhgb59$v1j$1@nntp.fujitsu-siemens.com...
Hi,
I'd like to bind a mouse button press to a canvas.
I tried
#! /usr/bin/perl
use warnings;
use strict;
use Tk;
sub MeldeDich {
print STDERR join(" ", @_), "\n";
}
# Create main window.
my $main = new MainWindow;
my $canvas = $main->Canvas(
-height => "12c",
-width => "16c",
-background => "blue",
);
$canvas->pack(-side=>"top", -fill=>"x");
$canvas->bind($canvas, '<Button-1>', [ \&MedleDich, "Hello\n" ]);
$canvas->bind($canvas, '<Button-1>', [ \&MedleDich, "Hello\n" ]);
MainLoop;
but I get no output ...
In the end, I'd like to know where on the canvas I have pressed the
mouse button (I'd like to select a region of a graph for zooming).
Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
Try:
use warnings;
use strict;
use Tk;
sub MeldeDich {
print STDERR join(" ", @_), "\n";
}
# Create main window.
my $main = new MainWindow;
my $canvas = $main->Canvas(
-height => "12c",
-width => "16c",
-background => "blue",
);
$canvas->pack(-side=>"top", -fill=>"x");
$main->bind($canvas, '<Button-1>', [ \&MeldeDich, "Hello\n" ]);
MainLoop;
Barry
| |
| Josef Moellers 2005-09-29, 7:56 am |
| Barry wrote:
> $main->bind($canvas, '<Button-1>', [ \&MeldeDich, "Hello\n" ]);
(rather than my $canvas->bind($canvas, ...)
Thanks, this now works.
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
| |
| Konstantin Tokar 2005-09-29, 6:58 pm |
| Josef Moellers wrote:
> Hi,
>
> I'd like to bind a mouse button press to a canvas.
[O'Reilly , "Mastering Perl/Tk" by Steve Lidie and Nancy Walsh]
9.4. Using bind with a Canvas
When you try to use the bind method with a Canvas widget, you can easily
run into unexpected problems. You may either get an error and your
script won't run, or your script will run but your bind won't seem to
have any effect. What's happening here is that Canvas objects have their
own special bind method that works with tags and item IDs. To get around
this, you'll need to use CanvasBind, a special method that binds events
to the canvas as a whole:
$canvas = $mw->Canvas( );
$canvas->CanvasBind("<Button-1>", sub { print "bind!\n"; });
If you used the Scrolled method to create your Canvas, you'll have an
added difficulty; you'll have to use the Subwidget method to get to the
Canvas widget:
$canvas = $mw->Scrolled("Canvas");
$real_canvas = $canvas->Subwidget("canvas");
$real_canvas->CanvasBind("<Button-1>", sub { print "bind!\n" });
Other than this one small annoyance, bind works just as you would
expect. This example prints the Canvas coordinate you clicked on:
$c = $mw->Scrolled("Canvas")->pack( );
$canvas = $c->Subwidget("canvas");
$canvas->CanvasBind("<Button-1>", [ \&print_xy, Ev('x'), Ev('y') ]);
sub print_xy {
my ($canv, $x, $y) = @_;
print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n";
}
To summarize, use bind to create event bindings to Canvas items, and use
CanvasBind to create event bindings to the Canvas widget.
| |
| Ala Qumsieh 2005-09-29, 6:58 pm |
| Josef Moellers wrote:
> $canvas->bind($canvas, '<Button-1>', [ \&MedleDich, "Hello\n" ]);
Here, you are calling Tk::Canvas::bind. What you need is
Tk::Canvas::CanvasBind (which is just an alias to Tk::bind):
$canvas->CanvasBind('<Button-1>', ...);
or equivalently:
$canvas->Tk::bind('<Button-1>', ...);
> In the end, I'd like to know where on the canvas I have pressed the
> mouse button (I'd like to select a region of a graph for zooming).
I've written an article about this very subject:
http://www.perltk.org/index.php?opt...id=13&Itemid=28
--Ala
|
|
|
|
|