wxpython: Transparent panel in wxpython

ぃ、小莉子 提交于 2019-12-14 03:57:46

问题


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.


回答1:


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)



回答2:


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()


来源:https://stackoverflow.com/questions/29941789/wxpython-transparent-panel-in-wxpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!