wx.StaticBitmap - 简单的透明度(mask,png,bmp?)

时间:2013-04-02 09:25:26

标签: python python-2.7 wxpython png

经过一周的持续失败后,我仍然无法完成一项简单的任务:加载带有alpha通道或白色背景的png(例如下面的示例)并让它在wx.StaticBitmap中保持透明度。

我稍后需要在wx.panel中。它应该保持这样或类似。

这是我的方法之一(白色背景):

def __init__(self, parent):
    wx.Panel.__init__(self, parent)
    self.loc = wx.Image("intro/image.png",wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    z = wx.Mask(self.loc, wx.WHITE) 
    self.loc.SetMask(z) 
    self.locopic = wx.StaticBitmap(self, -1, self.loc, (0, 0))

我读了很多关于这个话题的内容。我是垃圾。抱歉。我想我错过了一些明显的东西。 wx.MaskTransparent images

更新

我设法找到了at WorkinWithImages

的示例
import ImageConversions
...

    puFilename = "intro/imagealpha.png"
    pilImage = Image.open( puFilename )
    pilImageWithAlpha = ImageConversions.WxImageFromPilImage( pilImage, createAlpha=True )
    self.puWxBitmap = pilImageWithAlpha.ConvertToBitmap()
    self.locopic = wx.StaticBitmap(self, -1, self.puWxBitmap)

这是在PNX中创建透明的wx.image,在wx中使用alpha通道BUT.StaticBitmap会有透明度应该是丑陋的黑色。这让我感到震惊!!!请帮忙!

如果只是我设法在wx.panel中显示该图像的透明度在正确的位置 谢谢社区!

1 个答案:

答案 0 :(得分:4)

正如python SO聊天中所讨论的那样:

class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.loc = wx.Bitmap("intro/image.png")

    def OnPaint(self, evt):
        dc = wx.PaintDC(self)
        dc.SetBackground(wx.Brush("WHITE"))

        # ... drawing here all other images in order of overlapping
        dc.DrawBitmap(self.loc, 0, 0, True)

诀窍是使用wx.PaintDC绘制所有重叠图像。

此外,使用wx.Bitmap代替wx.Image(..., wx.BITMAP_TYPE_PNG).ConvertToBitmap()从文件系统加载PNG更方便。

相关问题