WxPython: wx.grid.Grid load buggy images

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,

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.

···

On May 19, 2019, at 2:39 PM, Youn-Bo younes.boutriq@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?

Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

When I uncomment that line the images get deleted.

···

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.

You're getting that error message because your MyImageRenderer doesn't implement the GetBestSize() method. This method is required.

Scott

···

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?

Ok, thank you. Any help on how to implement this method in my code. Maybe it will solve my problem.

···

On Monday, 20 May 2019 09:09:40 UTC-4, Scott Talbert wrote:

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

Here's an example from the demos:

···

On Mon, 20 May 2019, Youn-Bo wrote:

Ok, thank you. Any help on how to implement this method in my code. Maybe it
will solve my problem.

On Monday, 20 May 2019 09:09:40 UTC-4, Scott Talbert wrote:
      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

--
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.
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.

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.

···

On Monday, 20 May 2019 10:49:07 UTC-4, Scott Talbert wrote:

Here’s an example from the demos:

https://github.com/wxWidgets/Phoenix/blob/master/demo/GridStdEdRend.py#L37

On Mon, 20 May 2019, Youn-Bo wrote:

Ok, thank you. Any help on how to implement this method in my code. Maybe it

will solve my problem.

On Monday, 20 May 2019 09:09:40 UTC-4, Scott Talbert wrote:

  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

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 wxpytho...@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 wrote:

    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.

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)

···

On Monday, 20 May 2019 01:24:38 UTC-4, Tim Roberts wrote:

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thank a lot that was the error indeed.

···

On Monday, 20 May 2019 12:49:05 UTC-4, Tim Roberts wrote:

Youn-Bo wrote:

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

  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.