grid cell with GridCellAutoWrapStringRenderer/Editor

The Renderer does the auto wrap nicely when loading the grid, how can I force it to do it after changing a cell? Is there some GridTableMessage or similar for this?

I also have a problem that when I try to use both Renderer and Editor I get a hard crash without any exception.

I use the following code to set them

     def GetAttr(self, row, col, other):
         """Set attributes for a particular cell, e.g. readonly
         """
         if col in self.readOnlyColumns:
             attr =wx.grid.GridCellAttr()
             attr.SetReadOnly(True)
             attr.SetTextColour(wx.BLUE)
             return attr
         elif col == 2:
             # aligment for quantity column
             attr = wx.grid.GridCellAttr()
             attr.SetAlignment(wx.ALIGN_RIGHT, -1)
             return attr
         elif col == 5:
             attr = wx.grid.GridCellAttr()
             attr.SetRenderer(wx.grid.GridCellAutoWrapStringRenderer())
             attr.SetEditor(wx.grid.GridCellAutoWrapStringEditor())
             return attr

         return

I am on python 2.6 and wxPython 2.8.11.0 on Windows 7 64bit.

Anyone has some tips on this for me?

Werner

Some more googeling helped.

The Renderer does the auto wrap nicely when loading the grid, how can I force it to do it after changing a cell? Is there some GridTableMessage or similar for this?

Doing this in the OnCellChange event is doing the trick.
         # force autosize, need to use CallAfter otherwise it crashes
         wx.CallAfter(self.AutoSizeRows)

I also have a problem that when I try to use both Renderer and Editor I get a hard crash without any exception.

As a work around I now do it this way:

         self.RegisterDataType(wx.grid.GRID_VALUE_STRING,
                               wx.grid.GridCellAutoWrapStringRenderer(),
                               wx.grid.GridCellAutoWrapStringEditor())

I consider this a work around as I now get it on all string columns.

Was trying this too:

         attr = wx.grid.GridCellAttr()
         attr.SetRenderer(wx.grid.GridCellAutoWrapStringRenderer())
         attr.SetEditor(wx.grid.GridCellAutoWrapStringEditor())
         self.SetColAttr(5, attr)

But it resets the column width, autosize etc even so that stuff was done further down in the code, still within __init__ method of the grid.

So, I still wonder why it crashes when I try to set both of them within the "GetAttr" method of a PyGridTableBase.

Werner

···

On 24/09/2010 10:52, werner wrote:

My guess is that it has something to do with the reference counting scheme that the Grid classes use. Try calling IncRef on the editor and renderer. Another thing to try is to create the attribute object in the __init__ and then use the same instance each time GetAttr is called for col 5. You'll need to call IncRef on the attribute object before returning it in that case.

···

On 9/24/10 1:52 AM, werner wrote:

The Renderer does the auto wrap nicely when loading the grid, how can I
force it to do it after changing a cell? Is there some GridTableMessage
or similar for this?

I also have a problem that when I try to use both Renderer and Editor I
get a hard crash without any exception.

I use the following code to set them

def GetAttr(self, row, col, other):
"""Set attributes for a particular cell, e.g. readonly
"""
if col in self.readOnlyColumns:
attr =wx.grid.GridCellAttr()
attr.SetReadOnly(True)
attr.SetTextColour(wx.BLUE)
return attr
elif col == 2:
# aligment for quantity column
attr = wx.grid.GridCellAttr()
attr.SetAlignment(wx.ALIGN_RIGHT, -1)
return attr
elif col == 5:
attr = wx.grid.GridCellAttr()
attr.SetRenderer(wx.grid.GridCellAutoWrapStringRenderer())
attr.SetEditor(wx.grid.GridCellAutoWrapStringEditor())
return attr

--
Robin Dunn
Software Craftsman