WxPython: wx.grid.Grid load buggy images

174 views
Skip to first unread message

Youn-Bo

unread,
May 19, 2019, 9:26:44 PM5/19/19
to wxPython-users

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.

Ccw36.png


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,

Tim Roberts

unread,
May 20, 2019, 1:24:38 AM5/20/19
to 'Khanh D Dang' via wxPython-users
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.
— 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Youn-Bo

unread,
May 20, 2019, 1:31:20 AM5/20/19
to wxPython-users


On Monday, 20 May 2019 01:24:38 UTC-4, Tim Roberts wrote:
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.

When I uncomment that line the images get deleted.

Capture.PNG

 

Scott Talbert

unread,
May 20, 2019, 9:09:40 AM5/20/19
to wxPython-users
On Sun, 19 May 2019, Youn-Bo 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're getting that error message because your MyImageRenderer doesn't
implement the GetBestSize() method. This method is required.

Scott

Youn-Bo

unread,
May 20, 2019, 10:27:18 AM5/20/19
to wxPython-users
Ok, thank you. Any help on how to implement this method in my code. Maybe it will solve my problem. 

Scott Talbert

unread,
May 20, 2019, 10:49:07 AM5/20/19
to wxPython-users
Here's an example from the demos:
https://github.com/wxWidgets/Phoenix/blob/master/demo/GridStdEdRend.py#L37
> --
> 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-user...@googlegroups.com.
> To view this discussion on the web visithttps://groups.google.com/d/msgid/wxpython-users/5f62eb2f-e80b-4405-a7a2-7a
> 7d7b98af73%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>

Youn-Bo

unread,
May 20, 2019, 11:09:01 AM5/20/19
to wxPython-users
Now I receive: NotImplementedError: GridCellRenderer.Draw() is abstract and must be overridden

This problem is killing me. I have no idea what I'm doing. I'm a beginner and I bearly understand oop concepts so reading documentation is hard for me. Please, can someone take a few minutes of his time to test the script on his PC and see what is wrong with it? It would be greatly appreciated.

Tim Roberts

unread,
May 20, 2019, 12:49:05 PM5/20/19
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.

Youn-Bo

unread,
May 20, 2019, 2:14:49 PM5/20/19
to wxPython-users
Thank a lot that was the error indeed.
Reply all
Reply to author
Forward
0 new messages