wxpython:wxpython 中的透明面板 [英] wxpython: Transparent panel in wxpython

查看:119
本文介绍了wxpython:wxpython 中的透明面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 wxpython 中的 SetTransparent(val) 可以使整个框架透明.但是我可以在其中制作一个透明的面板吗?

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?

我尝试使用 panelobj.SetTransparent(val) 但没有用.

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

假设我想为 panel1 设置透明度.

supposing I want to set transparency for panel1.

推荐答案

哪些窗口可以透明,哪些不能透明是平台特定的.

It's platform-specific which windows can and can't be made transparent.

CanSetTransparent 方法允许您检查窗口的透明度是否可以在运行时切换.如果它返回 false,SetTransparent 将(通常)什么都不做,你不应该调用它.

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…):

  • 使用 Cocoa 构建的 Mac OS X:任何东西都可以透明.
  • 使用 Carbon 构建的 Mac OS X(不再使用):只有顶级窗口可以是透明的.
  • Windows:只有顶级窗口可以是透明的.
  • 带有合成器的 X11:任何东西都可以是透明的.
  • 没有合成器的 X11:没有什么是透明的.
  • Wayland(仍处于试验阶段):任何事情都可以透明.

然而,Windows 是一个特例.虽然您无法切换子窗口的透明度,或将其设置为百分比,但您可以在创建时使其完全透明,如 ρss 的答案.

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)

这篇关于wxpython:wxpython 中的透明面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆