1

コードは透明度のある他のpng画像で動作するように見えるため、問題の原因を画像に切り分けました。ただし、必要な1つの画像では機能しないようです。素敵な形の窓を作ろうとしているので、これは非常に役立ちます。

画像:

代替テキスト

コード:

import wx

class PictureWindow(wx.Frame):
    def __init__(self, parent, id, title, pic_location):

        # For PNGs. Must be PNG-8 for transparency...
        self.bmp = wx.Image(pic_location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()

        framesize = (self.bmp.GetWidth(), self.bmp.GetHeight())

        # Launch a frame the size of our image. Note the position and style stuff...
        # (Set pos to (-1, -1) to let the OS place it.
        # This style wx.FRAME_SHAPED is a frameless plain window.
        wx.Frame.__init__(self, parent, id, title, size=framesize, pos = (50, 50), style = wx.FRAME_SHAPED)

        r = wx.RegionFromBitmap(self.bmp)
        self.SetShape(r)

        # Define the panel and place the pic
        panel = wx.Panel(self, -1)
        self.mainPic = wx.StaticBitmap(panel, -1, self.bmp)

        # Set an icon for the window if we'd like
        #icon1 = wx.Icon("icon.ico", wx.BITMAP_TYPE_ICO)
        #self.SetIcon(icon1)

        self.Show()

        # The paint stuff is only necessary if doing a shaped window
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Main()


    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0, True)

    def Main(self):
        sizer = wx.GridBagSizer()
        button = wx.Button(self,-1,label="Click me !")
        sizer.Add(button, (0,1))


# What pic are we opening?
pic_location = r"C:\Users\user\Pictures\CPUBAR\A4.png" # PNG must be png-8 for             transparency...

app = wx.App(redirect=0) # the redirect parameter keeps stdout from opening a wx gui window
PictureWindow(None, -1, 'Picture Viewer', pic_location)
app.MainLoop()

これはWindows 7にあります。

4

4 に答える 4

7

wx.RegionFromBitmap は、ビットマップのマスクを使用して形状を設定します。ソース画像にアルファ チャネルがある場合、マスクがないため、wx.RegionFromBitmap は使用する形状を決定できません。すべてのピクセルが完全に不透明または完全に透明になるようにソース イメージを変換すると、wx.Image はアルファ チャネルの代わりにマスクを使用してそれを読み込みます。または、wx.Image の ConvertAlphaToMask メソッドを使用して実行時に変換してから、wx.Bitmap に変換することもできます。

于 2010-07-19T16:06:43.797 に答える
0

あなたのコードは、透明性が機能するためにそれがpng-8フォーマットである必要があると言っています。まず、画像はpng-8形式ですか?第二に、なぜこれが透明性の必要条件なのですか?

于 2010-07-19T07:56:31.777 に答える
0

回避策として、.gifを使用できます(限られたカラーセットに耐えられる場合)。これらは1ビットのアルファチャネルのみをサポートします。

于 2011-01-24T22:42:11.973 に答える
0

画像をビットマップに変換しています - ビットマップは透明度をサポートしていません。

于 2010-07-19T13:05:55.107 に答える