wx.grid.Grid 没有加载图片
wx.grid.Grid doesn't load image
我试图在此 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()
我得到的结果是:
您可以使用此图片进行测试:
我不知道您是否在 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'
根据这些,因为这个例子是旧的和过时的,我们可以用 GridCellRenderer
替换 PyGridCellRenderer
并完全转储 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
如果您遇到如下错误,那么您可能直接调用了 setlocale()
而不是使用 wxLocale
,造成了 C/C++ 和 Windows 之间的不匹配语言环境。
只能通过创建 wxLocale
对象来更改区域设置以避免这种情况!
在代码中使用这两行就可以正常工作了:
import locale
#after created the grid
locale.setlocale(locale.LC_ALL, 'C')
我试图在此 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()
我得到的结果是:
您可以使用此图片进行测试:
我不知道您是否在 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'
根据这些,因为这个例子是旧的和过时的,我们可以用 GridCellRenderer
替换 PyGridCellRenderer
并完全转储 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
如果您遇到如下错误,那么您可能直接调用了 setlocale()
而不是使用 wxLocale
,造成了 C/C++ 和 Windows 之间的不匹配语言环境。
只能通过创建 wxLocale
对象来更改区域设置以避免这种情况!
在代码中使用这两行就可以正常工作了:
import locale
#after created the grid
locale.setlocale(locale.LC_ALL, 'C')