Friends,
I can't figure out how to set a callback for a mouse click on my canvas. I have this code ...
#!/usr/bin/perl -w
use strict;
use Tk;
use Tk::Canvas;
use Getopt::Std;
my $width = 250;
my $height = 250;
my $units = 10;
my $background = 'blue';
my $fill = 'yellow';
my %opts = ();
getopts( 'W:H:b:f:u:h', \%opts );
if( $opts{W} ) { $width = $opts{W} ; }
if( $opts{H} ) { $height = $opts{H} ; }
if( $opts{b} ) { $background = $opts{b} ; }
if( $opts{f} ) { $fill = $opts{f} ; }
if( $opts{u} ) { $units = $opts{u} ; }
my $dx = $width / $units;
my $dy = $height / $units;
my $top = MainWindow->new();
my $can = $top->Canvas( -width => $width, -height=> $height )->pack();
drawGrid( $can );
my $root = "BLAHBLAHBLAH";
$can->bind( \$can, '<Button-1>', \&my_test( $root ) );
MainLoop;
sub drawGrid {
my $can = shift;
my $X=0;
for ( my $x=0; $x < $units; $x++ ) {
$can->create( 'line' , $X , 0 , $X , $can->reqheight()
+ ); $X += $dx;
}
my $Y=0;
for ( my $y=0; $y < $units; $y++ ) {
$can->create( 'line' , 0 , $Y , $can->reqwidth() , $Y)
+; $Y += $dy;
}
}
sub my_test {
my $msg = shift;
print "$msg\n";
}
... and the problem with this code is that the sub
my_test executes from the MainLoop starts and not when I click the mouse on the canvas. I want
my_test to execute when I click the mouse on the canvas.
Thanks
| Plankton: 1% Evil, 99% Hot Gas. |
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.