Search code examples
pythonwxpythonwxwidgets

How can a wxpython frame "steal" and "return" focus similar to the Dash app?


I want to make a heads-up display that pops up it's frame, taking focus from the previous application and then gives focus back to that application when it's done.

wxPython frames have Hide and Show methods that work but don't focus the application. Also, there's SetFocus which you'd think would do it but doesn't.

Update

I found a nasty hack that works. Rather than hide and show the frame, you save the app state and close the app instead of hiding. Then when you want to show again you spark a new one in a new multiprocessing.Process. Hardly ideal though. I hope someone has something better.

Clarification

To clarify, I'm trying to get the keyboard focus, not just to get the frame on top of the stack.

This issue might be specific to OS X.


Solution

  • You might be able to steal focus by calling Raise on the frame. There is the wx.STAY_ON_TOP style flag that could also be applied. Then you might be able to just use the frame's Show and Hide methods to make it work.

    Depending on what exactly you want to do, you might take a look at the ToasterBox widget. I can't recall if it actually steals focus, but it just pop up and go away on its own, which is handy in some cases.