如何在 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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)