如何在 Python 3 中使用具有透明度的 wxshape 窗口和 png?

问题描述

编辑:发现了如何在我的形状框架上放置一个透明wxPanel,但现在我尝试在wxGridBagSizer上放置一个wxGridBagSizer strong>wxPanel 有一些小部件,但只会出现第一个小部件......有任何想法吗?

原始问题:

好吧,我正在尝试制作一个 wx.ShapedWindowpng 背景 具有透明度,问题是它看起来像他们更改了 wx lib,以便在我们可以使用 wx.RegionFromBitmap() 之前在位图上设置形状,但这不再起作用,我尝试使用 wx.Region 只是,但我不知道如何获得透明度!我有一个灰色区域,我想要透明区域....

如果有人可以帮助我!

这是我现在得到的:

import wx
from os import getcwd

class ShapedFrame(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,None,-1,"Shaped Window",style = wx.FRAME_SHAPED | wx.SIMPLE_BORDER )
        self.hasShape = False
        self.delta = wx.Point(0,0)
        ImgDir = (getcwd()+u"\\img\\ex.png")
        self.bmp = wx.Image(ImgDir,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        w,h = self.bmp.GetWidth(),self.bmp.GetHeight()
        self.SetClientSize(w,h)
        #self.panel = wx.Panel(self,size=(100,100))
        panel = Panel(self)
        panel.Show()
        dc = wx.ClientDC(self)
        dc.DrawBitmap(self.bmp,True)
        self.SetwindowShape()
        self.Bind(wx.EVT_LEFT_DCLICK,self.ondoubleclick)
        self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP,self.OnLeftUp)
        self.Bind(wx.EVT_MOTION,self.OnMouseMove)
        self.Bind(wx.EVT_RIGHT_UP,self.OnExit)
        self.Bind(wx.EVT_PAINT,self.OnPaint)
        self.Bind(wx.EVT_WINDOW_CREATE,self.SetwindowShape)

################# EDIT ONLY BUTTON 0 WILL APPEAR ########################
        button0 = wx.Button(panel,"hello 0")
        button1 = wx.Button(panel,"hello 1")
        button2 = wx.Button(panel,"hello 2")
        button3 = wx.Button(panel,"hello 3")

        gBox7 = wx.GridBagSizer(0,0)
        gBox7.SetEmptyCellSize((10,10))
        gBox7.Add(button0,(0,0))
        gBox7.Add(button1,1))
        gBox7.Add(button2,(1,0))
        gBox7.Add(button3,1))

        panel.SetSizer(gBox7)

#######################################################################
    
    def SetwindowShape(self,evt=None):
        r = wx.Region(self.bmp,wx.TransparentColour)
        self.hasShape = self.SetShape(r)

    def ondoubleclick(self,evt):
        if self.hasShape:
            self.SetShape(wx.Region())
            self.hasShape = False
        else:
            self.SetwindowShape()

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

    def OnExit(self,evt):
        self.Close()

    def OnLeftDown(self,evt):
        self.CaptureMouse()
        pos = self.ClientToScreen(evt.GetPosition())
        origin = self.GetPosition()
        self.delta = wx.Point(pos.x - origin.x,pos.y - origin.y)

    def OnMouseMove(self,evt):
        if evt.Dragging() and evt.LeftIsDown():
            pos = self.ClientToScreen(evt.GetPosition())
            newPos = (pos.x - self.delta.x,pos.y - self.delta.y)
            self.Move(newPos)

    def OnLeftUp(self,evt):
        if self.HasCapture():
            self.ReleaseMouse()

class Panel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,size=(200,200,),style=wx.TRANSPARENT_WINDOW)
        self.CenterOnParent()


app = wx.App()

ShapedFrame(None,None).Show()

app.MainLoop()

解决方法

好的,我找到了一个解决方案,在 wx.coulor 中查找,因为 2.9 他们添加了 wxTransparentColour

所以我替换了:

r = wx.Region(self.bmp,"grey",30)

与:

r = wx.Region(self.bmp,wx.TransparentColour)

它工作正常!希望它也能帮助其他人:)