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:
import wximport 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()
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?
On May 19, 2019, at 2:39 PM, Youn-Bo <younes...@gmail.com> wrote: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?
You have a line of code in there to clear the entire cell before drawing the image, but you have it commented out. Why? I would think that's exactly what you are lacking here.
> email to wxpytho...@googlegroups.com.
That's because you have a typo. You have DrawRectangleRect instead of DrawRectangle, so it fails with an error and the image never gets drawn. If you had run this from a command line, you would have seen the errors:
Traceback (most recent call last):
File "x.py", line 58, in Draw
dc.DrawRectangleRect(rect)
AttributeError: 'PaintDC' object has no attribute 'DrawRectangleRect'
Traceback (most recent call last):
File "x.py", line 58, in Draw
dc.DrawRectangleRect(rect)
AttributeError: 'PaintDC' object has no attribute 'DrawRectangleRect'
Traceback (most recent call last):
File "x.py", line 58, in Draw
dc.DrawRectangleRect(rect)
-- Tim Roberts, ti...@probo.com Providenza & Boekelheide, Inc.