在Python中的wx.Frame外部捕获鼠标事件 [英] Capturing mouse events outside wx.Frame in Python

查看:138
本文介绍了在Python中的wx.Frame外部捕获鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用wxPython的Python中,如何根据鼠标相对于应用程序的窗口或框架的接近度来设置窗口的透明度和大小?

In Python using wxPython, how can I set the transparency and size of a window based on the proximity of the mouse relative to the application's window, or frame?

例如.类似于双曲线缩放,还是MAC OS X中的Dock?我正在尝试使用具有透明性和成形窗口的png来实现此效果.

Eg. similar to a hyperbolic zoom, or The Dock in MAC OS X? I am trying to achieve this effect with a png with transparency and a shaped window.

任何执行此操作的库或代码段也将很棒.谢谢.

Any libraries or code snippets that do this would be great too. Thanks.

推荐答案

此处是执行此操作的代码.基本上使用Infinity77提到的方法.在Windows上测试.效果很好!

Here's code to do it. Basically uses the approach mentioned by Infinity77. Tested on Windows. Works nicely!

import wx

MIN_ALPHA = 64
MAX_ALPHA = 255

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.alpha = MAX_ALPHA
        self.SetTitle('Mouse Alpha')
        self.on_timer()
    def on_timer(self):
        x, y, w, h = self.GetRect()
        mx, my = wx.GetMousePosition()
        d1 = max(x - mx, mx - (x + w))
        d2 = max(y - my, my - (y + h))
        alpha = MAX_ALPHA - max(d1, d2)
        alpha = max(alpha, MIN_ALPHA)
        alpha = min(alpha, MAX_ALPHA)
        if alpha != self.alpha:
            self.SetTransparent(alpha)
            self.alpha = alpha
        wx.CallLater(20, self.on_timer)

if __name__ == '__main__':
    app = wx.App(None)
    frame = Frame()
    frame.Show()
    app.MainLoop()

这篇关于在Python中的wx.Frame外部捕获鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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