With wxPython, how does one trigger an event whenever the whole window goes into/out of focus?
To elaborate, I'm building a serial terminal GUI and would like to close down the connection whenever the user doesn't have my application selected, and re-open the connection whenever the user brings my app back into the foreground. My application is just a single window derived from wx.Frame.Answer1:
The correct answer for this case is to use an EVT_ACTIVATE handler bound to the frame. There will be an event whenever the frame is activated (brought into the foreground relative to other windows currently open) or deactivated. You can use the event object's GetActive method to tell which just happened.Answer2:
as WxPerl programmer i know there is
if you initialize this event by listening to the frame as first parameter it should work as in Perl since the API is almost the sameAnswer3:
Interesting article at <a href="http://www.blog.pythonlibrary.org/2009/08/27/wxpython-learning-to-focus/" rel="nofollow">http://www.blog.pythonlibrary.org/2009/08/27/wxpython-learning-to-focus/</a>
Gist of it: wx.EVT_KILL_FOCUS works fine, but wx.EVT_SET_FOCUS behaves a little oddly for any panel containing widgets (the child's set-focus prevents the panel's set-focus event from firing as expected?)Answer4:
In addition to what these fellows are saying, you might also want to try EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW. I think these are fired when you move the mouse into and out of the frame widget, although I don't think the frame has to be in focus for those events to fire.
@ Hugh - thanks for the readership!