David Bailey wrote: > > > How do I get the same (x,y) coordinate for a given relative location in > the Ruby/tk application regardless of which widget I'm clicking on? > > Background: > > This has to do with a sudoku program I'm writing wherein I use the (x,y) > coordinates from mouse clicks to determine which sudoku cell I am in. > The cells are TkcRectangles and the numbers in (actually 'on') them are > TkLabels. Note that both the TkcRectangles and the TkLabels have the > same TkCanvas as their parent widget. > > When I click in a cell (TkcRectangle) with no number (TkLabel) displayed > within it, I get an (x,y) coordinate relative to the canvas (TkCanvas), > but > when I click in the same rectangle after I display a number within it > (placed > over the TkcRectangle as a TkLabel) - and I click on that number, then > the (x,y) coordinate reported back to me is relative to the TkLabel's > origin, not the TkCanvas'. > > This breaks my cell lookup scheme! > > Again, since both the rectangles and labels have the canvas a parent, I > don't > see why the (x,y) coordinates shouldn't be relative to the canvas in > both > cases. That is what I want anyway. Is there a way to make this this > happen? > > Does Ruby/Tk implement Tk's Virtual Desktop Coordinates (vrootx and > vrooty) that give coordinates relative to the virtual desktop? > > Or does Ruby/Tk implement accessing the rootx and rooty coordinates > which would be the same regardless of which widget is under the mouse > click? > > Or can I use a 'lower' method on the the TkLabel widget to put it below > the > TkcRectangles (without, of course, covering it up)? > > Alas, and alack, I've browsed around the docs quite a bit. I also tried > a bunch > of stuff, but I just keep breaking the code that I had running fine! > > I'm stuck!!! > > > > > The relevant code is straightforward. > > I”Ēve got a root and a canvas: > @root = TkRoot.new(:title=>"David's Sudoku Treasury", > :geometry=>"650x650") > @canvas = TkCanvas.new(@root, :width=> @canvasWidth, :height=> > @canvasHeight) > > A callback proc bind'ed to the mouse button: > @root.bind('Button', proc{|b,x,y| mainCellClickedProc(b, x, y)}, \ > "%b %x %y") > > A method that is called inside of the bind callback proc to report the > button clicked and the (x,y) coordinate of the clicked location: > def tell_it(btn, x, y) > puts "tell_it: Mouse button #{btn} clicked at coordinates > (#{x},#{y})" > end > > A set of 81 rectangles: > def createAndDrawCells() > # Sizing and Location Variables > @cellSize = 40 if @cellSize == 0 # Cell size > @xr1c1 = 10 if @xr1c1 == 0 # Cell R1C1 X Upper Left Position > @yr1c1 = 10 if @yr1c1 == 0 # Cell R1C1 Y Upper Left Position > @color = @bgColor > # Cell Rows > 9.times do |i| > xul = @xr1c1 > yul = @yr1c1 + (i * @cellSize) > xlr = xul + @cellSize > ylr = yul + @cellSize > # Cell Columns > 9.times do |j| > @boardRects [ (i * 9) + j ] = TkcRectangle.new(@canvas, xul, > yul, xlr, ylr, \ > :fill=>@color) > xul = xul + @cellSize > xlr = xlr + @cellSize > end > end > end > > > A set of 81 labels: > def createEntryNumberLabels() > 81.times do |i| > @entryNumberLabels[i] = TkLabel.new(@canvas) > end > end And the method to write the numbers (TkLabels) is: def writeEntryNumber(num, x, y, fgCol, bgCol) if @cellValues[calcCellFromXYCoords(x,y)] == nil entryNumText = " " else entryNumText = num.to_s end @entryNumberLabels[calcCellFromXYCoords(x, y)].configure( :text=> entryNumText, :font=> 'arial 16 bold', :foreground=> fgCol, :background=> bgCol ).place( :x=>x + 15, :y=>y + 25, :width=>20, :height=>20 ) end -- Posted via http://www.ruby-forum.com/.