I use a custom renderer to add an image to grid but the end result is really buggy and I was wondering if that was related to the error message I receive when running the script: NotImplementedError: GridCellRenderer.GetBestSize() is abstract and must be overridden If not, What should I do to have a clean display?
In the capture below you can see that:
- The images load with a gray background (line 1)
- The background disappears if the cell get clicked 2 times as to edit, and one click away from the cell (line 2)
- In line 3 I tried to expand the row vertically and it’s like the content of the following rows got imprinted on the expanded row.
Here is the code I use:
import wx
import wx.grid as gridlib
class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(None, -1, title = "wx.Grid - Bitmap example", size=(800, 600))
gridlib = wx.grid.Grid(frame)
gridlib.CreateGrid(17,17)
gridlib.SetColLabelValue(0, "Source")
gridlib.SetColLabelValue(1, "Campaign")
gridlib.SetColLabelValue(2, "")
gridlib.SetColLabelValue(3, "")
gridlib.SetColLabelValue(4, "IMPR.")
gridlib.SetColLabelValue(5, "Clicks.")
gridlib.SetColLabelValue(6, "CTR")
gridlib.SetColLabelValue(7, "Spent")
gridlib.SetColLabelValue(8, "CPM")
gridlib.SetColLabelValue(9, "CPC")
gridlib.SetColLabelValue(10, "GA REV.")
gridlib.SetColLabelValue(11, "GA IMPR.")
gridlib.SetColLabelValue(12, "GA CLICKS")
gridlib.SetColLabelValue(13, "GA CTR")
gridlib.SetColLabelValue(14, "GA RPM")
gridlib.SetColLabelValue(15, "Margin")
gridlib.SetColLabelValue(16, "Target CPC")
gridlib.SetColLabelValue(17, "Change CPC")
uk = wx.Bitmap(r"E:\Python\img\uk.png", wx.BITMAP_TYPE_ANY)
desktop = wx.Bitmap(r"E:\Python\img\dk.png", wx.BITMAP_TYPE_ANY)
def RenderImage(col, row, img):
gridlib.SetCellRenderer(col,row,MyImageRenderer(img))
gridlib.SetRowSize(col,img.GetHeight()+2)
gridlib.SetColSize(row,img.GetWidth()+2)
for i in range(0,17):
RenderImage(i,2,uk)
RenderImage(i,3,desktop)
frame.Show(True)
return True
class MyImageRenderer(wx.grid.GridCellRenderer):
def __init__(self, img):
wx.grid.GridCellRenderer.__init__(self)
self.img = img
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
image = wx.MemoryDC()
image.SelectObject(self.img)
dc.SetBackgroundMode(wx.SOLID)
if isSelected:
dc.SetBrush(wx.Brush(wx.BLUE, wx.SOLID))
dc.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID))
else:
dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID))
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID))
#dc.DrawRectangleRect(rect)
width, height = self.img.GetWidth(), self.img.GetHeight()
if width > rect.width-2:
width = rect.width-2
if height > rect.height-2:
height = rect.height-2
dc.Blit(rect.x+1, rect.y+1, width, height, image, 0, 0, wx.COPY, True)
app = MyApp(0)
app.MainLoop()
``
Thank you,