如果将不透明度设置为零,wxpython 的 SetTransparent 不会捕获用户输入

发布于 2024-11-19 20:40:18 字数 967 浏览 0 评论 0原文

我正在尝试制作一个 wxpython 窗口(只是一个窗口,因为它是一个窗口对象)..它填充整个屏幕并且完全不可见。然后我想允许用户在“窗口”(即屏幕上的任何位置)内单击并拖动。

当我尝试执行 self.SetTransparent(0) 时,窗口不会捕获用户输入。

这是有意的行为吗?

这是实现我想要的目标的正确方法吗?不透明度为 1 显然人眼无法区分,但我仍然很好奇为什么不能使其完全透明。

这是片段:

import wx

class Frame(wx.Frame):
    def __init__(self):
        style = (wx.STAY_ON_TOP | wx.NO_BORDER)
        wx.Frame.__init__(self, None, title="Invisible", style=style)

        self.SetTransparent(0) # This doesn't work
        #self.SetTransparent(1) # But this works fine

        self.Bind(wx.EVT_KEY_UP, self.OnKeyPress)

    def OnKeyPress(self, event):
        """quit if user press q or Esc"""
        if event.GetKeyCode() == 27 or event.GetKeyCode() == ord('Q'): #27 is Esc
            self.Close(force=True)
        else:
            event.Skip()

app = wx.App()
frm = Frame()
frm.ShowFullScreen(True)
app.MainLoop()

或者有没有办法让窗口根本没有背景,而不是完全透明的彩色背景?

I'm trying to make a wxpython window (only a window in the sense that it's a window object).. that fills the entire screen and is completely invisible. I then want to allow the user to click and drag within the "window" (ie. anywhere on the screen).

When I try doing self.SetTransparent(0) user input doesn't get captured by the window.

Is this intended behaviour?

Is this the correct way to achieve what I want? An opacity of 1 is obviously indistinguishable to the human eye, but I'm still curious as to why I can't make it completely transparent.

Here's the snippet:

import wx

class Frame(wx.Frame):
    def __init__(self):
        style = (wx.STAY_ON_TOP | wx.NO_BORDER)
        wx.Frame.__init__(self, None, title="Invisible", style=style)

        self.SetTransparent(0) # This doesn't work
        #self.SetTransparent(1) # But this works fine

        self.Bind(wx.EVT_KEY_UP, self.OnKeyPress)

    def OnKeyPress(self, event):
        """quit if user press q or Esc"""
        if event.GetKeyCode() == 27 or event.GetKeyCode() == ord('Q'): #27 is Esc
            self.Close(force=True)
        else:
            event.Skip()

app = wx.App()
frm = Frame()
frm.ShowFullScreen(True)
app.MainLoop()

Or is there a way of giving the window no background at all rather than a completely transparent coloured background?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2024-11-26 20:40:18

您可以覆盖 EVT_ERASE_BACKGROUND 来实现相同的效果。

我还清理了代码的其他方面。

XP 与 7 上的行为略有不同,但对于您所描述的应用程序类型来说可能不是问题。

import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
    def OnEraseBackground(self, event):
        pass # do nothing
    def OnLeftDown(self, event):
        print event.GetPosition()
    def OnKeyDown(self, event):
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Close()
        else:
            event.Skip()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.ShowFullScreen(True)
    app.MainLoop()

You can override EVT_ERASE_BACKGROUND to accomplish the same effect.

I also cleaned up other aspects of the code.

Behaves slightly differently on XP versus 7, but probably not an issue for the type of app you're describing.

import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
    def OnEraseBackground(self, event):
        pass # do nothing
    def OnLeftDown(self, event):
        print event.GetPosition()
    def OnKeyDown(self, event):
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Close()
        else:
            event.Skip()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.ShowFullScreen(True)
    app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文