GridCellAttr.SetBackgroundColour change all cell color...

Hi all.

I want to change only selected cell color. But it change all cell color.
The problem I’m having is that when I call SetBackgroundColour the cell always change all cell on grid.

Please advise me.

Here is the event handler I have attached to wx.grid.EVT_GRID_SELECT_CELL:

def onSelected(self, event):

selected_row = event.GetRow()

selected_col = event.GetCol()

attr = self.mygrid.GetOrCreateCellAttr(selected_row, selected_col)

attr.SetBackgroundColour(random.choice([‘yellow’, ‘red’, ‘black’]))

self.mytable.SetAttr(attr, selected_row, selected_col)

self.mygrid.ForceRefresh()

event.Skip()

Hello,

does grid.SetCellBackgroundColour work?

def onSelected(self, event):
    selected_row = event.GetRow()
    selected_col = event.GetCol()

    self.mygrid.SetCellBackgroundColour(random.choice(['yellow', 'red', 'black']))

    self.mygrid.ForceRefresh()
    event.Skip()

``

I found SetCellBackgroundColour, SetBackgroundColour can work if I do not use wxgrid.PyGridTableBase GetAttr.

def init(self)
self.font_attr = wx.grid.GridCellAttr()

self.font_attr.SetFont(wx.Font(HEX_FONT_SIZE, wx.DEFAULT, wx.NORMAL, wx.LIGHT, encoding=wx.FONTENCODING_SYSTEM))

self.font_attr.SetTextColour(TEXT_COLOUR)

def GetAttr(self, row, col, kind):

attr = self.font_attr

attr.IncRef()

return attr

How should I write wxgrid.PyGridTableBase GetAttr?

2015年11月1日日曜日 17時58分19秒 UTC+9 Torsten:

···

Hello,

does grid.SetCellBackgroundColour work?

def onSelected(self, event):
    selected_row = event.GetRow()
    selected_col = event.GetCol()

    self.mygrid.SetCellBackgroundColour(random.choice(['yellow', 'red', 'black']))

    self.mygrid.ForceRefresh()
    event.Skip()

``

Hello,

it seems to me that all your Cells share the same GridCellAttr(). If you want to change the background for one cell only, you need to clone the GridCellAttr, change the background colour of the new GridCellAttr and SetAttr(newAttr, selected_row, selected_col).
I attach a sample app, which hopefully will show it to you.

GridCustTable.py (6.47 KB)

Thank you, Torsten.

I understand that attr.clone can copy one cell.
But when I use GetAttr for attr setting all cell, attr.clone is not effective.

I cannot change the background for one cell only in this case.

How do I in this case?

I make a test code from your code.

2015年11月3日火曜日 3時34分52秒 UTC+9 Torsten:

GridCustTable.py (6.7 KB)

···

Hello,

it seems to me that all your Cells share the same GridCellAttr(). If you want to change the background for one cell only, you need to clone the GridCellAttr, change the background colour of the new GridCellAttr and SetAttr(newAttr, selected_row, selected_col).
I attach a sample app, which hopefully will show it to you.

In your example CustomDataTable.GetAttr() always returns the same GridCellAttr (CustomDataTable.color_attr). That’s why all your cells have the same backgroundcolour. You can’t have one GridCellAttr with different backgroundcolours. If you wan’t one cell with another backgroundcolour, you need to have another GridCellAttr.

Isn’t the core of the problem that the OP is mixing two concepts?
E.g. see the demo: GridSimple.py vs. GridCustTable.py or
Grid_MegaExample.py.
table.SetAttr can be used with GridSimple.py.
The attribute will be stored in the table. The table will hold all
the data and attributes.
This will work for relatively small tables.
But for a grid with a CustomDataTable(gridlib.PyGridTableBase) the
attributes are not stored in the grid.
They need to be returned by CustomDataTable.GetAttr instead whenever
a cell is to be rendered.
In the example code here, OnLeftClick calls GetOrCreateCellAttr.
This will call CustomDataTable.GetAttr, which will return the single
shared instance and then this is either

  • modified in place and so the whole grid is affected or
  • cloned and modified and so this does not have any effect, as the
    next call to GetAttr will return the shared instance again.
    So the CustomDataTable.GetAttr needs to check whether the cell is in
    the selection or not and return the appropriate
    attributes.
    Regards,
    Dietmar
···

Am 05.11.2015 um 22:45 schrieb Torsten:

In your example CustomDataTable.GetAttr() always returns the same
GridCellAttr (CustomDataTable.color_attr ). That’s
why a ll your cells have the same backgroundcolour. You
can’t have one GridCellAttr with different backgroundcolours. If
you wan’t one cell with another backgroundcolour, you need to
have another GridCellAttr.