Capturing mouse events outside wx.Frame in Python

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

Eg. similar to hyperbolic scaling or docking in MAC OS X? I am trying to achieve this effect using png with transparency and a shaped window.

Any libraries or code snippets that do this will also be good. Thanks.

+4
source share
2 answers

Here is the code for this. Mostly the approach mentioned by Infinity77 is used. Tested on Windows. It works well!

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() 
+6
source

I do not think that this can be done so easily if the mouse is outside the main frame. However, you can always do the following:

1) Start the timer in your main frame and poll it every 50 milliseconds (or whatever suits you);

2) After you interrogate it in the OnTimer event handler, check the mouse position via wx.GetMousePosition () (this will be in the coordinates );

3) In the same OnTimer method, get the screen position of your frame through frame.GetScreenPosition ();

4) Compare the position of the mouse with the position of the frame (possibly using the calculation of the Euclidean distance or what suits you). Then you set the transparency of the frame accordingly to this distance (do not forget to leave it completely opaque if the mouse is inside the rectangle of the frame).

I did it just for fun, it shouldn't take more than 5 minutes.

Hope this helps.

Andrea.

+2
source

Source: https://habr.com/ru/post/1401409/


All Articles