Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Tcl/Tk mouse events

by Sandy (Curate)
on Mar 04, 2016 at 04:18 UTC ( #1156788=perlquestion: print w/replies, xml ) Need Help??

Sandy has asked for the wisdom of the Perl Monks concerning the following question:


To use the Eval from Tcl, it must be the first parameters passed to the callback function.


$cn->toplevel->bind( '<ButtonPress>', [ sub { my ( $x, $y, $cn ) = @_; $lx = $x; $ly = $y; _start_draw($cn); }, Tcl::Ev('%x', '%y'), $cn, ] );

Ok, I am stumped

Checking what it takes to convert to Tcl/Tk from Perl/Tk.

Was good, until I tried to work with a mouse event

How do I get the position of the mouse?

Tried many things, (latest shown below), but can't seem to get what I need. Anybody know?

$cn->toplevel->bind( '<ButtonPress>', [ sub {print "hello sandy\n"; print "anonymous: ",join(", ",@_),"\n"; my ( $cn, $x, $y ) = @_; $lx = $x; $ly = $y; _start_draw($cn); }, $cn, $int->Eval('set lx %x'), Ev('%y') ] );

just tried $int->Ev which returns a Tcl::Ev event... not quite sure what to do with yet...

Replies are listed 'Best First'.
Re: Tcl/Tk mouse events
by Anonymous Monk on Mar 04, 2016 at 05:50 UTC
    How do I get the position of the mouse?

    Does this help?

    Canvas item selection by mouse click
    There are 2 problems when looking for items at a given position:

    On mouse click, you get window coordinates, but you need canvas coordinates for "find".

    Canvas find returns only the ID of the item, that's a plain number. You will need to retrieve the parameters of the item by further functions.


    canvas .c1 bind .c1 <Double-1> { onDblClick %x %y } proc onDblClick {x y} { set x [.c1 canvasx $x] ; set y [.c1 canvasy $y] set i [.c1 find closest $x $y] set t [.c1 gettags $i] puts "$i $t" }
Re: Tcl/Tk mouse events
by Anonymous Monk on Mar 04, 2016 at 07:46 UTC

    What module are you using?


        I see. So the info that Tcl::Ev() should come first can be found as example in,

        $T->tagBind(qw/demo <Motion>/ => [sub { my($x, $y) = (shift,shift); my($text, $sv) = @_; #my $e = $text->XEvent; #my($x, $y) = ($e->x, $e->y); my $new_line = $text->index("\@$x,$y linestart"); if ($new_line ne $last_line) { $text->tagRemove(qw/hot 1.0 end/); $last_line = $new_line; $text->tagAdd('hot', $last_line, "$last_line lineend"); } show_stat $sv, $text, $text->index('current'); }, Tcl::Ev('%x','%y'), \$STATUS_VAR] );

        It is also documented in Tcl

        4. As a special case, there is a mechanism to deal with Tk's special event variables (they are mentioned as '%x', '%y' and so on throughout Tcl). When creating a subroutine reference that uses such variables, you must declare the desired variables using Tcl::Ev as the first argument to the subroutine.
Re: Tcl/Tk mouse events
by vkon (Curate) on Mar 06, 2016 at 17:41 UTC
    actually most "mouse events" are better done on pure-tcl level, only calling your perl calback when necessary - this is my approach.

    but - yes, your approach is also possible, you only must give to tcl/tk an idea that you're waiting for '%x,%y' or '%X,%Y' event variables.

    you will have much quicker responces if you'll ask this question to devoted mailing list - tcltk at perl dot org.


Re: Tcl/Tk mouse events
by vkon (Curate) on Mar 06, 2016 at 17:45 UTC
    ...and I will add here that "Tcl::pTk" promises much better support of perl/Tk compatibility, although I haven't really verified that, and I prefer minimalistic modules that solve the task, rather than having thick full 100% compatibility layer that support different levels of old bugs.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1156788]
Approved by Tanktalus
Front-paged by Tanktalus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-12-04 19:51 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.