wxPython - 将透明的 PNG 图像放在另一个图像之上
wxPython - Putting a transparent PNG image on top of another
我需要在普通图片上显示透明的 .png 图像。我试过 this 方法,但是 wxPython 说
wxPaintDC can't be created outside wxEVT_PAINT handler
我找不到解决方法。我将 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()
谢谢!
找到解决方案。只需要以正确的方式使用 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()
我需要在普通图片上显示透明的 .png 图像。我试过 this 方法,但是 wxPython 说
wxPaintDC can't be created outside wxEVT_PAINT handler
我找不到解决方法。我将 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()
谢谢!
找到解决方案。只需要以正确的方式使用 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()