From: Chris Alfeld Date: 2006-04-03T09:34:07+09:00 Subject: Re: Ruby/Tk (X,Y) Coordinate Question canvasx and canvasy translate from coordinates relative to the canvas widget to the canvas's coordinate system. By default this will be the same but if you have scrollbars or other transformations it can differ. I have never found a good use for root_x and root_y coordinates. Try to avoid using coordinates at all when possible. Tk has a very rich binding ability which means you can often do everything you want without every worrying about actual coordinates of mouse clicks. The exception is canvas work where you use %x %y fed through canvasx and canvasy to get canvas coordinates. Writing fully portable Tk code is difficult (though possibly easier than with certain other GUI toolkits). I use the packer (.pack) almost exclusively. Only when I'm trying to do something really unusual do I use the placer (.place). The packer does have a bit of a learning curve but is very powerful and easy once learned properly. Don't try to pack too many widgets into the same parent. Subdivide into frame's as needed. While the perl/tk interface is closer to ruby/tk's than Tcl/Tk's the Tcl/Tk documentation is the ultimate source for how the widgets actually behave. This is especially true for an implementation like Ruby/Tk which not only wraps compiled code but also wraps Tcl code. You should find a copy either only or download (activestate.com has a nice online copy). Use this documentation is "what does this do?" and use the Perl/Tk documentation for "how do I use it?". On 4/2/06, David Bailey wrote: > David Bailey wrote: > > Chris Alfeld wrote: > >> Actually, what I think you are looking for is the translation from > >> "real" coordinates to canvas coordinates for which you want: > >> > >> canvas.canvasx and canvas.canvasy > >> > >> also note that you can bind mouse clicks to Tkc objects, though be > >> careful that if the fill is none then TkcRect's won't register clicks > >> on the interior. > >> > >> So, in summary, see TkCanvas.canvasx, TkCanvas.canvasy, and > >> TkcRectangle.bind > >> (actually part of the TkcTagAccess mixin). > >> > >> Another command you may find useful is TkCanvas.find_overlapping which > >> can translate a mouse click to a TkcObject. Thus code such as: > >> > >> obj_at_xy = > >> @canvas.find_overlapping(@canvas.canvasx(x),@canvas.canvasy(y), > >> @canvas.canvasx(x),@canvas.canvasy(y)) > >> > >> Here I believe x,y should be from %x,%y, that is, relative to the canvas > >> widget. > >> > >> c. > > > > > > Chris, > > > > I'm researching these canvasx and canvasy methods now. I'll let you > > know how I make out. Thank you for your help. > > > > David > > Hey Chris and/or Fellow Rubyists, > > I bought and flipped through "Mastering Perl/Tk" by Nancy Walsh and > Steve Lidie, which was purported to be "the" book on Perk/Tk. There was > scant info on canvasx, canvasy and find_overlapping. I read through > what was there, though. > > Both canvasx and canvasy take a single argument, but when I supplied > various x and y arguments, the methods just returned the same argument > as a result! So, I guess I just don't get it. Do you have a sense of > what the argument is supposed to be and what is supposed to happen to > it? > > Also, where can I get some inormation about find_overlapping, real > quick. > > I spent all day yesterday fighting with widget placement (see my earlier > post "Placing Sets of Ruby/Tk Widgets"), and that pooped me out. What > week-end brain power I have left, I will apply toward continuing to > explore this problem. > > Thanks, > > David > > > P.S. BTW, my use of root_x and root_y coordinates did not work because > they appear to be relative to the desktop and not the application > window. (Unless I did something drastically wrong!) So, if I calculate > an offset but the user then moves the application window, the offset > becomes no longer valid. Rats! > > -- > Posted via http://www.ruby-forum.com/. > >