Wxpython: Transparent Panel In Wxpython
Solution 1:
Yes it is possible. You can use style=wx.TRANSPARENT_WINDOW
Sample code: I made the panel1
transparent and gave green color to topPanel
. Thats why you see green color on top of panel2
because the panel1
is transparent.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title,size=(250, 250))
topPanel = wx.Panel(self, -1)
topPanel.SetBackgroundColour('green')
panel1 = wx.Panel(topPanel, -1, style=wx.TRANSPARENT_WINDOW)
#panel1.SetTransparent(100)
panel2 = wx.Panel(topPanel, -1)
panel2.SetBackgroundColour('gray')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel1,1,flag = wx.EXPAND|wx.ALL)
sizer.Add(panel2,1,flag = wx.EXPAND|wx.ALL)
topPanel.SetSizer(sizer)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'frame')
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
Solution 2:
It's platform-specific which windows can and can't be made transparent.
The CanSetTransparent
method allows you to check whether a window's transparency can be toggled at runtime. If it returns false, SetTransparent
will (usually) do nothing, and you shouldn't call it.
Off the top of my head (but don't quote me on this—it must be documented somewhere, but I can't find it…):
- Mac OS X with Cocoa build: anything can be transparent.
- Mac OS X with Carbon build (which nobody uses anymore): only top-level windows can be transparent.
- Windows: only top-level windows can be transparent.
- X11 with compositor: anything can be transparent.
- X11 without compositor: nothing can be transparent.
- Wayland (which is still experimental): anything can be transparent.
However, Windows is a special case. While you can't toggle a sub-window's transparency, or set it to a percentage, you can make it fully transparent at creation time, as shown in ρss's answer.
So, if that's what you're looking for, and you want to do it as portably as possible, you'll want something like this:
style = wx.TRANSPARENT_WINDOW if sys.platform.lower() == 'win32'else0
panel1 = wx.Panel(topPanel, -1, style=style)
if panel1.CanSetTransparent:
panel1.SetTransparent(100)
Post a Comment for "Wxpython: Transparent Panel In Wxpython"