簡體   English   中英

wx.grid.Grid 不加載圖像

[英]wx.grid.Grid doesn't load image

我正在嘗試使用本教程中的代碼,但結果是單元格變灰且單元格中沒有圖像(請參見屏幕截圖)。 自從我開始尋找將圖像添加到網格單元的解決方案已經有幾天了,我發現這個解決方案迄今為止最簡單,但它對我不起作用。 拜托,有人可以幫我解決這個問題,以便我可以繼續我的項目嗎? 這將不勝感激。 謝謝你。

這是代碼:

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 中運行它,但是如果您在命令行上運行它,您將看到所有警告和錯誤。 IE

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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM