Help with TypeError when calling GetScrollPixelsPerUnit on Grid

Hi Folks,

I’m hitting an error in some code which has previously worked for me. It’s been a while since I updated to a newer wxPython.

The code is:

    def MakeCellVisibleCentered(self, row):
        cell_coords = self.CellToRect(row, 0)
        client_size = self.GetGridWindow().GetClientSize()
        ycoord_pixels = cell_coords.y - (client_size.height / 2)
        **ycoord = ycoord_pixels / wx.ScrolledWindow.GetScrollPixelsPerUnit(self)[1]**
        if ycoord < 0:
            ycoord = 0
        wx.ScrolledWindow.Scroll(self, 0, ycoord)

The error is:

TypeError: _ScrolledWindowBase.GetScrollPixelsPerUnit(): first argument of unbound method must have type ‘_ScrolledWindowBase’

In this function, self is a wx.grid.Grid. The documentation seems to say that Grid derives from Scrolled. This code is just trying to force a given row to become visible in the scrolled window. If there’s a better way to do this, I would appreciate knowing that.

Thanks!!!
-Jim.

well, I haven’t got a grid, only a wx.ScrolledWindow like

        # data table
        self.scrwin = wx.ScrolledWindow(panel)
        self.scrwin.SetScrollRate(40, 40)
        self.insert_rows()

and any time I test

print(self.scrwin.GetScrollPixelsPerUnit())

I get the tuple (40, 40), so the base of your grid must have changed
try __class__.__bases__ or __class__.__mro__ to find out about the relation
or method ‘GetScrollLineY’ of wx.grid.Grid (less interesting but more focussed)

The C++ classes related to scrolled windows had some hierarchy changes some years back (templates, mixin-classes and a few layers of inheritance) which made it tricky to automatically wrap the C++ classes and also keep compatibility with previous versions on the Python side. So I instead did some hacks and workarounds that worked with the new C++ classes and also maintained compatibility from Python.

So from Python’s perspective many of the methods that are actually in one of the base or template C++ classes will appear to be implemented in the derived class instead. When the method is called it will actually be routed to the proper C++ method correctly, but it does lead to some unexpected quirks like what’s shown above.

Turns out I could just call these methods on self directly (since they derive from Scrolled) rather than trying to use the wx path.Not sure why I did it that way years ago but the new way is way cleaner.