http://qs1969.pair.com?node_id=1156788

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

UPDATE

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

Example:

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

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.

    Example:

    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?

      Tcl::Tk

        I see. So the info that Tcl::Ev() should come first can be found as example in http://search.cpan.org/grep?cpanid=VKON&release=Tcl-Tk-1.04&string=bind&i=1&n=1&C=9, https://metacpan.org/source/VKON/Tcl-Tk-1.04/tk-demos/widget

        $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.

    regards,
    Vadim

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.