Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxpython: Transparent panel in wxpython

It is possible to make the whole frame transparent using SetTransparent(val) in wxpython. But can I make a single panel in it to be transparent?

I tried using panelobj.SetTransparent(val) but that didnt work.

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)

        panel1 = wx.Panel(topPanel, -1)
        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()

supposing I want to set transparency for panel1.

like image 233
Deepa Avatar asked Sep 05 '26 15:09

Deepa


2 Answers

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()
like image 94
ρss Avatar answered Sep 08 '26 03:09

ρss


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' else 0
panel1 = wx.Panel(topPanel, -1, style=style)
if panel1.CanSetTransparent:
    panel1.SetTransparent(100)
like image 41
abarnert Avatar answered Sep 08 '26 03:09

abarnert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!