【问题标题】:wx.grid.Grid doesn't load imagewx.grid.Grid 不加载图像
【发布时间】:2019-10-05 20:02:11
【问题描述】:

我正在尝试在此tutorial 中使用代码,但结果是一个灰色的单元格,并且单元格中没有图像(参见屏幕截图)。自从我开始寻找将图像添加到网格单元格的解决方案已经有好几天了,我发现这个解决方案是迄今为止最简单的解决方案,但它对我不起作用。拜托,有人可以帮我解决这个问题,以便我继续我的项目吗?这将不胜感激。谢谢你。

代码如下:

import wx
import wx.grid
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(None, -1, title = "wx.Grid - Bitmap example")
        grid = wx.grid.Grid(frame)
        grid.CreateGrid(1,1)
        img = wx.Bitmap(r"E:\Dropbox2\Dropbox\Ubot\Ubot\Python\Magnify\Tkinter Magnify\Tests\python-logo.png", wx.BITMAP_TYPE_PNG)
        imageRenderer = MyImageRenderer(img)
        grid.SetCellRenderer(0,0,imageRenderer)
        grid.SetColSize(0,img.GetWidth()+2)
        grid.SetRowSize(0,img.GetHeight()+2)
        frame.Show(True)
        return True

class MyImageRenderer(wx.grid.PyGridCellRenderer):
    def __init__(self, img):
        wx.grid.PyGridCellRenderer.__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()

我得到的结果:

您可以使用此图片进行测试:

【问题讨论】:

    标签: python wxpython wxgrid


    【解决方案1】:

    我不知道您是否在 IDE 中运行它,但如果您在命令行上运行它,您将看到所有警告和错误。即

    wxPyDeprecationWarning: Using deprecated class. Use GridCellRenderer instead.
      wx.grid.PyGridCellRenderer.__init__(self)
    Traceback (most recent call last):
      File "20190519.py", line 30, in Draw
        dc.DrawRectangleRect(rect)
    AttributeError: 'PaintDC' object has no attribute 'DrawRectangleRect'
    

    基于这些,由于示例陈旧且过时,我们可以将PyGridCellRenderer 替换为GridCellRenderer 并完全转储dc.DrawRectangleRect(rect) 行。如果该功能不存在,请尝试不要使用它,如果不起作用,请寻找替代方法。

    编辑:那行应该是dc.DrawRectangle(rect)

    我们最终得到了这个:

    import wx
    import wx.grid
    class MyApp(wx.App):
        def OnInit(self):
            frame = wx.Frame(None, -1, title = "wx.Grid - Bitmap example")
            grid = wx.grid.Grid(frame)
            grid.CreateGrid(2,2)
            img = wx.Bitmap("wxPython.jpg", wx.BITMAP_TYPE_ANY)
            imageRenderer = MyImageRenderer(img)
            grid.SetCellRenderer(0,0,imageRenderer)
            grid.SetColSize(0,img.GetWidth()+2)
            grid.SetRowSize(0,img.GetHeight()+2)
            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.DrawRectangle(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()
    

    这给了我们这个:

    完整的可下载文档可在此处获得:https://extras.wxpython.org/wxPython4/extras/4.0.4/wxPython-docs-4.0.4.tar.gz
    演示在这里:
    https://extras.wxpython.org/wxPython4/extras/4.0.4/wxPython-demo-4.0.4.tar.gz

    【讨论】:

    • 非常感谢。它就像一个魅力,但我仍然在终端中收到此错误:“NotImplementedError: GridCellRenderer.GetBestSize() 是抽象的,必须被覆盖”
    • 不确定。我没有得到那个错误。我想这可能取决于您正在运行的版本。我在 wx 4.0.4
    【解决方案2】:

    如果您收到如下错误,那么您可能直接调用了setlocale(),而不是使用wxLocale,造成C/C++ 和Windows 语言环境不匹配。

    只有通过创建 wxLocale 对象来更改语言环境才能避免这种情况!

    在代码中使用这两行就可以正常工作了:

    import locale
    
    #after created the grid
    
    locale.setlocale(locale.LC_ALL, 'C')
    

    【讨论】:

      猜你喜欢
      • 2022-10-13
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2016-06-23
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多