简体   繁体   English

wxpython:wxpython中的透明面板

[英]wxpython: Transparent panel in wxpython

It is possible to make the whole frame transparent using SetTransparent(val) in wxpython. 使用wxpython中的SetTransparent(val)可以使整个框架透明。 But can I make a single panel in it to be transparent? 但是我可以使其中一个面板透明吗?

I tried using panelobj.SetTransparent(val) but that didnt work. 我尝试使用panelobj.SetTransparent(val)但是没有用。

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. 假设我要为panel1设置透明度。

Yes it is possible. 对的,这是可能的。 You can use style=wx.TRANSPARENT_WINDOW 您可以使用style = wx.TRANSPARENT_WINDOW

Sample code : I made the panel1 transparent and gave green color to topPanel . 示例代码 :我使panel1透明,并给topPanel绿色。 Thats why you see green color on top of panel2 because the panel1 is transparent. 这就是为什么您在panel2顶部看到绿色的panel2因为panel1是透明的。

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

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. CanSetTransparent方法允许您检查是否可以在运行时切换窗口的透明度。 If it returns false, SetTransparent will (usually) do nothing, and you shouldn't call it. 如果返回false,则SetTransparent (通常)将不执行任何操作,并且您不应调用它。

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. 带有Cocoa构建的Mac OS X:任何东西都可以透明。
  • Mac OS X with Carbon build (which nobody uses anymore): only top-level windows can be transparent. 带有Carbon版本的Mac OS X(不再使用):只有顶层窗口可以透明。
  • Windows: only top-level windows can be transparent. Windows:仅顶级窗口可以是透明的。
  • X11 with compositor: anything can be transparent. X11与合成器:任何东西都可以透明。
  • X11 without compositor: nothing can be transparent. X11没有合成器:没有东西可以透明。
  • Wayland (which is still experimental): anything can be transparent. Wayland(尚在试验中):任何事物都可以透明。

However, Windows is a special case. 但是,Windows是一种特殊情况。 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 . 虽然无法切换子窗口的透明度或将其设置为百分比,但是可以在创建时使其完全透明,如ρss的答案所示。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM