【问题标题】:wxPython - Putting a transparent PNG image on top of anotherwxPython - 将透明的 PNG 图像放在另一个之上
【发布时间】:2021-11-26 05:18:51
【问题描述】:

我需要在普通图片上显示一个透明的 .png 图像。我试过this 方法,但是 wxPython 说

wxPaintDC 不能在 wxEVT_PAINT 处理程序之外创建

我找不到解决方法。我将 wx.EVT_PAINT 绑定到某个函数并在那里尝试了代码,但没有成功。这是我的代码:

import wx

class Test(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.SetTitle('Testing transparency')
        self.baseImage = wx.StaticBitmap(self, wx.ID_ANY)

        self.DrawBaseImage()
        self.DrawOnTop()

    def DrawBaseImage(self):
        bitmap = wx.Bitmap('path', wx.BITMAP_TYPE_ANY)
        image = wx.Bitmap.ConvertToImage(bitmap)
        self.baseImage.SetBitmap(image.ConvertToBitmap())

    def DrawOnTop(self):
        bmp = wx.StaticBitmap(self.baseImage, wx.ID_ANY)

        bitmap = wx.Bitmap('path_of_transparent_image', wx.BITMAP_TYPE_PNG)
        image = wx.Bitmap.ConvertToImage(bitmap)
        bmp.SetBitmap(image.ConvertToBitmap())


app = wx.App()
Test(None).Show()
app.MainLoop()

谢谢!

【问题讨论】:

    标签: image wxpython transparency


    【解决方案1】:

    找到了解决办法。只需要以正确的方式使用 wx.EVT_PAINT!

    import wx
    
    class Test(wx.Panel):
        def __init__(self, parent):
            super().__init__(parent)
    
            self.base = wx.Bitmap('base', wx.BITMAP_TYPE_ANY)
            self.png = wx.Bitmap('transparent_image', wx.BITMAP_TYPE_PNG)
    
            self.Bind(wx.EVT_PAINT, self.OnPaint)
    
        def OnPaint(self, e):
            dc = wx.PaintDC(self)
            dc.SetBackground(wx.Brush("WHITE"))
    
            dc.DrawBitmap(self.base, 0, 0, True)
            dc.DrawBitmap(self.png, 0, 0, True)
    
    
    class Frame(wx.Frame):
        def __init__(self, parent):
            super().__init__(parent)
            Test(self).Show()
    
    app = wx.App()
    Frame(None).Show()
    app.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 1970-01-01
      • 2016-03-12
      • 2013-12-15
      • 1970-01-01
      • 2011-05-24
      • 2011-10-18
      • 1970-01-01
      • 2011-07-20
      相关资源
      最近更新 更多