Bug in XYToCell?

Copy and try the code here below:

import wx, wx.grid

class TestFrame(wx.Frame):

def __init__(self):
   
    wx.Frame.__init__(self, parent = None, id = -wx.ID_ANY,
            style = wx.MINIMIZE_BOX | wx.CLOSE_BOX| wx.SYSTEM_MENU\
                >wx.CAPTION  | wx.CLIP_CHILDREN)
    self.panel = wx.Panel(self)
    self.mainsizer = wx.BoxSizer(wx.VERTICAL)
    self.panel.SetSizer(self.mainsizer)
    self.grid = wx.grid.Grid(self.panel, wx.ID_ANY )
    self.mainsizer.Add(self.grid, 1, wx.ALL |  wx.GROW, 2)
    self.grid.CreateGrid(10, 6)
    self.grid.GetTable().SetValue(2, 1, 'row 3, col B')
    self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMouseMotion)
    self.Layout()
    self.mainsizer.SetSizeHints(self)
    self.mainsizer.Fit(self)
    self.Fit()
    self.grid.DefaultToolTip = ''
    self.Show()
   
    self.grid.AppendRows(5)       
   
def OnMouseMotion(self, event):
    event.Skip()
    pos = event.GetPosition()
    coords = self.grid.XYToCell(pos.x, pos.y)
    cell = coords[0], coords[1]
    if cell == (2, 1):
        self.grid.GetGridWindow().SetToolTipString('This is the tooltip of cell(3, B)')
    else:
        self.grid.GetGridWindow().SetToolTip(None)

if name == ‘main’:
app = wx.PySimpleApp()
frame = TestFrame()
app.MainLoop()

If you pass with your mouse over cell(2, 1), you’ll see the proper tooltip.
Now, scroll the grid a bit downward , so that the cell(2, 1) is still visible, and retry: you will not see the tooltip in the cell (3, B), but somehow below, as if the coordinates referred not to the cell but to an absolute position on the grid.
Any suggestion?

raffaello wrote:

Copy and try the code here below:

import wx, wx.grid

class TestFrame(wx.Frame):

    def __init__(self):
               wx.Frame.__init__(self, parent = None, id = -wx.ID_ANY,
                style = wx.MINIMIZE_BOX | wx.CLOSE_BOX| wx.SYSTEM_MENU\
                    >wx.CAPTION | wx.CLIP_CHILDREN)
        self.panel = wx.Panel(self)
        self.mainsizer = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(self.mainsizer)
        self.grid = wx.grid.Grid(self.panel, wx.ID_ANY )
        self.mainsizer.Add(self.grid, 1, wx.ALL | wx.GROW, 2)
        self.grid.CreateGrid(10, 6)
        self.grid.GetTable().SetValue(2, 1, 'row 3, col B')
        self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMouseMotion)
        self.Layout()
        self.mainsizer.SetSizeHints(self)
        self.mainsizer.Fit(self)
        self.Fit()
        self.grid.DefaultToolTip = ''
        self.Show()
               self.grid.AppendRows(5)
           def OnMouseMotion(self, event):
        event.Skip()
        pos = event.GetPosition()
        coords = self.grid.XYToCell(pos.x, pos.y)
        cell = coords[0], coords[1]
        if cell == (2, 1):
            self.grid.GetGridWindow().SetToolTipString('This is the tooltip of cell(3, B)')
        else:
            self.grid.GetGridWindow().SetToolTip(None)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = TestFrame()
    app.MainLoop()

If you pass with your mouse over cell(2, 1), you'll see the proper tooltip.
Now, scroll the grid a bit downward , so that the cell(2, 1) is still visible, and retry: you will not see the tooltip in the cell (3, B), but somehow below, as if the coordinates referred not to the cell but to an absolute position on the grid.
Any suggestion?

Yeah, it's annoying. I ran into this issue last year and Robin pointed me in the right direction. I don't completely understand it, but here's how to fix your code:

<code>

def OnMouseMotion(self, event):
    event.Skip() x, y = self.grid.CalcUnscrolledPosition(event.GetX(),event.GetY())
    coords = self.grid.XYToCell(x, y)
    cell = coords[0], coords[1]
    if cell == (2, 1):
        self.grid.GetGridWindow().SetToolTipString('This is the tooltip of cell(3, B)')
    else:
        self.grid.GetGridWindow().SetToolTip(None)

</code>

Notice that I deleted the line "pos = event.GetPosition()" and added the following:

x, y = self.grid.CalcUnscrolledPosition(event.GetX(),event.GetY())

As I understand it, this will get the x/y coordinates taking into account the grid that is not seen whereas the code you used only gets the position of the viewable grid at any given moment. Hopefully that makes sense...

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Thanks a lot, Mike, it works perfectly.
My only remark (not to you), even if English is not my native language: the code would be clearer if the method was not called CalcUnscrolledPosition. What’s in a name? That which we call a rose and so on, but if we call ‘stinky’ a rose it is a problem to sell it :slight_smile:

···

2009/1/22 Mike Driscoll mike@pythonlibrary.org

raffaello wrote:

Copy and try the code here below:

import wx, wx.grid

class TestFrame(wx.Frame):

def __init__(self):

          wx.Frame.__init__(self, parent = None, id = -wx.ID_ANY,

            style = wx.MINIMIZE_BOX | wx.CLOSE_BOX| wx.SYSTEM_MENU\

                >wx.CAPTION  | wx.CLIP_CHILDREN)

    self.panel = wx.Panel(self)

    self.mainsizer = wx.BoxSizer(wx.VERTICAL)

    self.panel.SetSizer(self.mainsizer)

    self.grid = wx.grid.Grid(self.panel, wx.ID_ANY )

    self.mainsizer.Add(self.grid, 1, wx.ALL |  wx.GROW, 2)

    self.grid.CreateGrid(10, 6)

    self.grid.GetTable().SetValue(2, 1, 'row 3, col B')

    self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMouseMotion)

    self.Layout()

    self.mainsizer.SetSizeHints(self)

    self.mainsizer.Fit(self)

    self.Fit()

    self.grid.DefaultToolTip = ''

    self.Show()

          self.grid.AppendRows(5)                def OnMouseMotion(self, event):

    event.Skip()

    pos = event.GetPosition()

    coords = self.grid.XYToCell(pos.x, pos.y)

    cell = coords[0], coords[1]

    if cell == (2, 1):

        self.grid.GetGridWindow().SetToolTipString('This is the tooltip of cell(3, B)')

    else:

        self.grid.GetGridWindow().SetToolTip(None)

if name == ‘main’:

app = wx.PySimpleApp()

frame = TestFrame()

app.MainLoop()

If you pass with your mouse over cell(2, 1), you’ll see the proper tooltip.

Now, scroll the grid a bit downward , so that the cell(2, 1) is still visible, and retry: you will not see the tooltip in the cell (3, B), but somehow below, as if the coordinates referred not to the cell but to an absolute position on the grid.

Any suggestion?

Yeah, it’s annoying. I ran into this issue last year and Robin pointed me in the right direction. I don’t completely understand it, but here’s how to fix your code:

def OnMouseMotion(self, event):

event.Skip() x, y = self.grid.CalcUnscrolledPosition(event.GetX(),event.GetY())

coords = self.grid.XYToCell(x, y)

cell = coords[0], coords[1]

if cell == (2, 1):

   self.grid.GetGridWindow().SetToolTipString('This is the tooltip of cell(3, B)')

else:

   self.grid.GetGridWindow().SetToolTip(None)

Notice that I deleted the line “pos = event.GetPosition()” and added the following:

x, y = self.grid.CalcUnscrolledPosition(event.GetX(),event.GetY())

As I understand it, this will get the x/y coordinates taking into account the grid that is not seen whereas the code you used only gets the position of the viewable grid at any given moment. Hopefully that makes sense…


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Python Extension Building Network: http://www.pythonlibrary.org


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users