How to set cell size on a grid which uses gridlib.PyGridTableBase as data source

I have a grid which uses gridlib.PyGridTableBase as data source. I want to merge some cells with the method below, so that cell (1,10) will span across 5 columns and 1 row:

self.SetCellSize(1, 10, 1, 5)

But when I call this method, the grid messes up and becomes unresponsive. Please help.

Best regards

Please help me with this.

···

On Saturday, January 3, 2015 at 5:08:54 PM UTC+2, steve wrote:

I have a grid which uses gridlib.PyGridTableBase as data source. I want to merge some cells with the method below, so that cell (1,10) will span across 5 columns and 1 row:

self.SetCellSize(1, 10, 1, 5)

But when I call this method, the grid messes up and becomes unresponsive. Please help.

Best regards

Apparently some of the magic is (or was) undocumented. This came up in a Google search - https://groups.google.com/forum/#!msg/wxpython-users/z4iobAKq0os/zzUL70WzL_AJ

Maybe you need to do something similar in your case?

HTH a little. Good luck.

Cheers,

Scott.

···

On Sat, Jan 3, 2015 at 10:08 AM, steve oslocourse@gmail.com wrote:

I have a grid which uses gridlib.PyGridTableBase as data source. I want to merge some cells with the method below, so that cell (1,10) will span across 5 columns and 1 row:

self.SetCellSize(1, 10, 1, 5)

But when I call this method, the grid messes up and becomes unresponsive. Please help.

Best regards

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Please provide more code, e.g. make a running sample we can try out.
Werner

···

Hi Steve,

  On 1/11/2015 17:01, steve wrote:

Please help me with this.

    On Saturday, January 3, 2015 at 5:08:54 PM UTC+2, steve wrote:

I have a grid which uses gridlib.PyGridTableBase
as data source. I want to merge some cells with the
method below, so that cell (1,10) will span across 5 columns
and 1 row:

                      self.SetCellSize(1,

10, 1, 5)

        But when I call this method, the grid messes up and becomes

unresponsive. Please help.

http://wiki.wxpython.org/MakingSampleApps

Dear Sir,

Please see the code below, when you right click on any cell on grid, it attempts to set size of cell (1,1) to 1 row and 3 columns with the self.SetCellSize(1, 1, 1, 3) method, but then the grid messes up:

import wx
import wx.grid as gridlib

···

#---------------------------------------------------------------------------

class HugeTable(gridlib.PyGridTableBase):

def __init__(self, log):
    gridlib.PyGridTableBase.__init__(self)
    self.log = log

    self.odd=gridlib.GridCellAttr()
    self.odd.SetBackgroundColour("sky blue")
    self.even=gridlib.GridCellAttr()
    self.even.SetBackgroundColour("sea green")

def GetAttr(self, row, col, kind):
    attr = [self.even, self.odd][row % 2]
    attr.IncRef()
    return attr


# This is all it takes to make a custom data table to plug into a
# wxGrid.  There are many more methods that can be overridden, but
# the ones shown below are the required ones.  This table simply
# provides strings containing the row and column values.

def GetNumberRows(self):
    return 50

def GetNumberCols(self):
    return 50

def IsEmptyCell(self, row, col):
    return False

def GetValue(self, row, col):
    return str( (row, col) )

def SetValue(self, row, col, value):
    self.log.write('SetValue(%d, %d, "%s") ignored.\n' % (row, col, value))

#---------------------------------------------------------------------------

class HugeTableGrid(gridlib.Grid):
def init(self, parent, log):
gridlib.Grid.init(self, parent, -1)

    table = HugeTable(log)

    # The second parameter means that the grid is to take ownership of the
    # table and will destroy it when done.  Otherwise you would need to keep
    # a reference to it and call it's Destroy method later.
    self.SetTable(table, True)

    self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnRightDown) 

def OnRightDown(self, event):
    print "hello"
    self.SetCellSize(1, 1, 1, 3)

#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
def init(self, parent, log):
wx.Frame.init(self, parent, -1, “Huge (virtual) Table Demo”, size=(640,480))
grid = HugeTableGrid(self, log)

    grid.SetReadOnly(5,5, True)

#---------------------------------------------------------------------------

if name == ‘main’:
import sys
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop()

#---------------------------------------------------------------------------

What version of wxPython are you using?
With your code and using wxPython 2.9.5 I get this exception:
File ,
line 64, in
File ,
line 1976, in
File ,
line 19, in
File ,
line 852, in
It doesn’t allow to set the cell size when it overlaps another
cell. Maybe change your code to this:
self.SetCellSize(event.GetRow(), event.GetCol(), 1, 1)
But then I don’t know what you would like to accomplish when right
clicking a cell.
Werner

···

Hi Steve,

  On 1/15/2015 12:30, steve wrote:

Dear Sir,

    Please see the code below, when you right click on any cell on

grid, it attempts to set size of cell (1,1) to 1 row and 3
columns with the self.SetCellSize(1, 1, 1, 3) method,
but then the grid messes up:

"d:\devOther\samplesTest\steve\pygridtable.py"OnRightDown
self.SetCellSize(1, 1, 1, 3)
"c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\grid.py"SetCellSize
return
_grid.Grid_SetCellSize(*args, **kwargs)
"d:\devOther\samplesTest\steve\pygridtable.py"GetAttr
attr.IncRef()
"c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx_core.py"IncRef
return
core.RefCounter_IncRef(*args, **kwargs)

  wx._core.PyAssertionError:

C++ assertion “!((cell_rows < 1) || (cell_cols < 1))” failed
at …..\src\generic\grid.cpp(7931) in wxGrid::SetCellSize():
wxGrid::SetCellSize setting cell size that is already part of
another cell

Hi Dear Werner,

I use 2.9.5.0 and Windows 7.
I don’'t get such exception. I binded function self.SetCellSize(1, 1, 1, 3) on rgiht click event, so that when user right clicks anywhere on grid, the cell on row 1, col 1 will span over 3 columns, and its size will be 1 row and 3 columns. But it fails, the grid becomes unresponsive.

···

On Friday, January 16, 2015 at 12:39:26 PM UTC+2, werner wrote:

Hi Steve,

  On 1/15/2015 12:30, steve wrote:

Dear Sir,

    Please see the code below, when you right click on any cell on

grid, it attempts to set size of cell (1,1) to 1 row and 3
columns with the self.SetCellSize(1, 1, 1, 3) method,
but then the grid messes up:

What version of wxPython are you using?

With your code and using wxPython 2.9.5 I get this exception:



File "d:\devOther\samplesTest\steve\pygridtable.py"    ,

line 64, in OnRightDown

  self.SetCellSize(1, 1, 1, 3)

File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\grid.py"    ,

line 1976, in SetCellSize

        return

_grid.Grid_SetCellSize(*args, **kwargs)

File "d:\devOther\samplesTest\steve\pygridtable.py"    ,

line 19, in GetAttr

  attr.IncRef()

File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py"    ,

line 852, in IncRef

        return

core.RefCounter_IncRef(*args, **kwargs)

      wx._core.PyAssertionError:

C++ assertion “!((cell_rows < 1) || (cell_cols < 1))” failed
at …..\src\generic\grid.cpp( 7931) in wxGrid::SetCellSize():
wxGrid::SetCellSize setting cell size that is already part of
another cell

It doesn't allow to set the cell size when it overlaps another

cell. Maybe change your code to this:

self.SetCellSize(event.GetRow(), event.GetCol(), 1, 1)



But then I don't know what you would like to accomplish when right

clicking a cell.

Werner

Strange, as you see I am on Windows too, also 8.1, but that should
not make a difference, so I don’t understand why you do not see the
exception. Maybe someone else will enlighten us.
Based on the exception text “” I would say that
it is not supported to size a cell in a why that it would overlap an
existing cell.
Can you explain why you would want to have row 1 span 3 columns on
right click of any cell?
Werner

···

Hi Steve,

  On 1/16/2015 16:32, steve wrote:

Hi Dear Werner,

    I use 2.9.5.0 and Windows 7.

    I don''t get such exception.
    I binded function self.SetCellSize(1, 1, 1, 3) on

rgiht click event, so that when user right clicks anywhere on
grid, the cell on row 1, col 1 will span over 3 columns, and its
size will be 1 row and 3 columns.

  setting cell

size that is already part of another cell

Hi Werner,

I just want to know how to use self.SetCellSize() method on a grid which is based on PyGridTableBase class.

···

On Saturday, January 17, 2015 at 11:16:15 AM UTC+2, werner wrote:

Hi Steve,

  On 1/16/2015 16:32, steve wrote:

Hi Dear Werner,

    I use 2.9.5.0 and Windows 7.

    I don''t get such exception.
Strange, as you see I am on Windows too, also 8.1, but that should

not make a difference, so I don’t understand why you do not see the
exception. Maybe someone else will enlighten us.

    I binded function self.SetCellSize(1, 1, 1, 3) on

rgiht click event, so that when user right clicks anywhere on
grid, the cell on row 1, col 1 will span over 3 columns, and its
size will be 1 row and 3 columns.

Based on the exception text " setting cell
size that is already part of another cell " I would say that
it is not supported to size a cell in a why that it would overlap an
existing cell.

Can you explain why you would want to have row 1 span 3 columns on

right click of any cell?

Werner

Hello

Win7 (64Bit), Python 2.7.9 (32Bit), 2.9.5.0 msw (classic)

I have the assertion error too. I don’t have a final answer, but maybe a hint. If i comment out GetAttr in HugeTable, then SetCellSize won’t assert. So i would say it has something to do with the Attribute of the cell an the reference counter.

And a self.Refresh() is needed after SetCellSize.

Torsten

Torsten wrote:

Hello

Win7 (64Bit), Python 2.7.9 (32Bit), 2.9.5.0 msw (classic)

I have the assertion error too. I don't have a final answer, but maybe a
hint. If i comment out GetAttrin HugeTable, then SetCellSizewon't
assert. So i would say it has something to do with the Attribute of the
cell an the reference counter.

And a self.Refresh()is needed after SetCellSize.

Part of the cell spanning is implemented by setting values in the cells' GridAttr objects, and since HugeTable.GetAttr was causing two attr objects to be shared by all cells in odd/even rows then it was confusing things when the SetCellSize was expecting to get unique attr objects instead, because it would set the values in the first affected cell, and then when it tried to do it for the next if found that it had already been set and so it raised the assertion assuming that it was already part of another spanning group.

That doesn't mean that you can't use cell spanning and attribute providing with a custom table all at the same time, but you will need to be smarter about how the attribute objects are provided.

···

--
Robin Dunn
Software Craftsman

Dear Sir Dunn,

Why isn’t spanning attribute provided as a standard feature in a wxpython grid with a GridTableBase?

Best regards

···

On Thursday, January 22, 2015 at 4:26:30 AM UTC+2, Robin Dunn wrote:

Torsten wrote:

Hello

Win7 (64Bit), Python 2.7.9 (32Bit), 2.9.5.0 msw (classic)

I have the assertion error too. I don’t have a final answer, but maybe a

hint. If i comment out GetAttrin HugeTable, then SetCellSizewon’t

assert. So i would say it has something to do with the Attribute of the

cell an the reference counter.

And a self.Refresh()is needed after SetCellSize.

Part of the cell spanning is implemented by setting values in the cells’
GridAttr objects, and since HugeTable.GetAttr was causing two attr
objects to be shared by all cells in odd/even rows then it was confusing
things when the SetCellSize was expecting to get unique attr objects
instead, because it would set the values in the first affected cell, and
then when it tried to do it for the next if found that it had already
been set and so it raised the assertion assuming that it was already
part of another spanning group.

That doesn’t mean that you can’t use cell spanning and attribute
providing with a custom table all at the same time, but you will need to
be smarter about how the attribute objects are provided.


Robin Dunn

Software Craftsman

http://wxPython.org

steve wrote:

Dear Sir Dunn,

Why isn't spanning attribute provided as a standard feature in a
wxpython grid with a GridTableBase?

I didn't say that it wasn't. I said that col or row spanning is tracked in the attribute objects for a cell, and so you need to be smarter about how you manage the attributes if you are providing attributes from your table class. Instead of sharing one or a few instances for all cells you need to ensure that you are not sharing attribute instances with cells that have spanning and those that don't.

···

--
Robin Dunn
Software Craftsman