我正在尝试在此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()
得到的结果是
您可以将此图像用于测试:
答案 0 :(得分: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
答案 1 :(得分:0)
如果您收到如下错误,那么您可能直接调用了 setlocale()
而不是使用 wxLocale
,造成 C/C++ 和 Windows 语言环境不匹配。
只能通过创建 wxLocale
对象来更改语言环境以避免这种情况!
在代码中使用这两行就可以了:
import locale
#after created the grid
locale.setlocale(locale.LC_ALL, 'C')