Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

I don't understand a piece of Perl/Tk code

by spx2 (Deacon)
on Aug 16, 2007 at 13:53 UTC ( [id://633048]=perlquestion: print w/replies, xml ) Need Help??

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

let's have $tg a Tk::TixGrid object,and let's consider <Button-1> and <B1-Motion> two constants wich are indicating to events produced by mouce clicks and ShowCell the subrotuine run at those certain events.What does Ev mean in this context ? In the source I have these 2 lines are used.And Ev is never defined nowhere in the source.I can also assure that they are not defined in any other modules included in there.

$tg->bind('<Button-1>', [\&ShowCell, Ev('x'), Ev('y')]); $tg->bind('<B1-Motion>', [\&ShowCell, Ev('x'), Ev('y')]);

Replies are listed 'Best First'.
Re: I don't understand a piece of Perl/Tk code
by zentara (Archbishop) on Aug 16, 2007 at 14:54 UTC
    What does Ev mean in this context ?..........Ev is never defined nowhere in the source.....

    Those are X window events. They are mentioned in perldoc Tk::bind under the Events section, but they are built so deep into the Tk windowing system, that it is probably in the Tcl c code, from which Perl/Tk is derived. If you look at perldoc Tk::Widget, you will see that every Tk widget has a $widget->pointerx (y) attribute. That is the x and y mouse pointer location relative to the widget itself...i.e. 0,0 is the widget's upper left corner. Tk bind has built in events, for the mouse and keypress activity, called Ev('x'), Ev('y'), Ev('k') and you can bind to those events. Additionally, most widgets will have a method to determine which widget element is located at any of it's pointerx and pointery locations. Here is a simple example:

    #!/usr/bin/perl use warnings; use strict; use Tk; my $dx; my $dy; my $mw = MainWindow->new; $mw->geometry("700x600"); my $canvas = $mw->Canvas(-width => 700, -height => 565, -bg => 'black', -borderwidth => 3, -relief => 'sunken', )->pack; my $closebutton = $mw->Button(-text => 'Exit', -command => sub{Tk::exi +t(0)}) ->pack; my $dragster = $canvas->createRectangle(0, 20, 50, 75, -fill => 'red', -tags => ['move'], ); $canvas->bind('move', '<1>', sub {&mobileStart();}); $canvas->bind('move', '<B1-Motion>', sub {&mobileMove();}); $canvas->bind('move', '<ButtonRelease>', sub {&mobileStop();}); MainLoop; sub mobileStart { my $ev = $canvas->XEvent; ($dx, $dy) = (0 - $ev->x, 0 - $ev->y); $canvas->raise('current'); print "START MOVE-> $dx $dy\n"; } sub mobileMove { my $ev = $canvas->XEvent; $canvas->move('current', $ev->x + $dx, $ev->y +$dy); ($dx, $dy) = (0 - $ev->x, 0 - $ev->y); print "MOVING-> $dx $dy\n"; my $color = $canvas->itemcget($dragster,'-fill'); print "color -> $color\n"; #or use bbox here for odd shapes.. bounding box my @coords = $canvas->coords($dragster); print "coords-> @coords\n"; } sub mobileStop{&mobileMove;}

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      Hi! Very nice piece of code. Altough i still have questions regarding it. In particular,you apply method move to $canvas altough you want to move $dragster. There is a hint about this in the fact that move() method has a parameter called "current",is this indicating that $dragster is the current object that has focus or... ?

      Now i have a second question. Is there first parameter in bind user-defined ,or is it from some list of possible values ? is the first argument to bind('move') in any way linked to the method move wich is applied to canvas ?

        You need to understand how the canvas tag system works. The dragster IS NOT an object, so it cannot have methods. I could make it into object, with some package code, but in general, "items" on a canvas, are elements of the canvas widget.

        Your second question is about canvas tags also. There is a difference between the canvas's bind and the normal Tk::bind. The canvas's bind will let you bind to a tag. If you look at each canvas item's creation, you will see it is given a tag(s). This is the basis of the canvas's power, because you can do all sorts of trickery with addtag and deltag to change an item's tags dynamically. So you can do things like setup a callback when a mouse enters any item with a certain tag. Like this:

        #!/usr/bin/perl use Tk; my $mw = MainWindow->new(); $mw->geometry("600x400+100+100"); $canvas = $mw->Canvas(width => 600, height => 400)->pack(); $canvas->Tk::bind("<Button-1>", [ \&print_xy, Ev('x'), Ev('y') ]); # This draws half of an oval $oval = $canvas->createOval(100,100,200,250,-fill => 'white',-tags => +"blue"); $canvas->Tk::bind("<Button-3>", sub {$canvas->itemconfigure($oval,-fil +l => "red")}); $canvas->bind('blue', '<Enter>', sub { $canvas->itemconfigure("blue", -fill => "blue"); } ) +; # sub {exit}); # When the mouse is not over, color it black. $canvas->bind("blue", "<Leave>", sub { $canvas->itemconfigure("blue", -fill => "black"); }); #$tw->tagBind('tag','<Enter>',sub{$overtag=1}); MainLoop; sub print_xy { print "@_\n"; my ($canv, $x, $y) = @_; print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n" +; }

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: I don't understand a piece of Perl/Tk code
by rir (Vicar) on Aug 16, 2007 at 14:06 UTC
    From the Perl/Tk Pocket Reference:

    Binding callbacks can be nested using the Ev(...) constructor. Ev(...) inserts callback objects into the argument list. When Perl/Tk prepares the argument list for the callback it is about to call it spots these special objects and recursively applies the callback process to them.

    Be well,
    rir

Re: I don't understand a piece of Perl/Tk code
by Anonymous Monk on Aug 16, 2007 at 14:28 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://633048]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found