2013-04-02 253 views
2

經過1周的不斷的失敗,我仍然無法做一個簡單的任務:加載一個帶有alpha通道或白色背景的png(在下面的示例中)並保持其在wx.StaticBitmap中的透明度。wx.StaticBitmap - 簡單的透明度(mask,png,bmp?)

這個我以後需要在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) 

這是從創建alpha通道,但WX一個PNG透明wx.image。在透明度應該是的情況下,StaticBitmap會有一個難看的黑色。這是駕駛我maaad!請幫助!

如果只是我會設法在wx.panel中顯示該圖像在正確的位置的透明度 謝謝社區!

回答

4

如蟒討論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更方便。

+0

非常感謝您花費了您的時間,並找到了我理解並實施成功的解決方案!謝謝! – Laci

+0

我得到這個錯誤:'wx._core.wxAssertionError:C++ assertion「IsOk()」在SetTextBackground(/tmp/pip-build-neujnbmg/wxPython/ext/wxWidgets/src/common/dcgraph.cpp(437)失敗):wxGCDC(cg):: SetTextBackground - 無效的DC'☹ – Sardathrion