wxPython全窗口焦点事件
使用 wxPython,当整个窗口进入/失去焦点时如何触发一个事件?
详细地说,我正在构建一个串行终端 GUI,并且希望在用户没有选择我的应用程序时关闭连接,并在用户将我的应用程序带回前台时重新打开连接。我的应用程序只是一个从 wx.Frame 派生的单个窗口。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这种情况的正确答案是使用绑定到框架的 EVT_ACTIVATE 处理程序。每当框架被激活(相对于当前打开的其他窗口进入前台)或停用时,都会发生一个事件。您可以使用事件对象的 GetActive 方法来判断刚刚发生了什么。
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.
作为 WxPerl 程序员,我知道有
EVT_SET_FOCUS(
EVT_KILL_FOCUS(
如果您通过侦听帧作为第一个参数来初始化此事件,它应该像在 Perl 中一样工作,因为 API 几乎相同
as WxPerl programmer i know there is
EVT_SET_FOCUS(
EVT_KILL_FOCUS(
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 same
有趣的文章 http://www.blog.pythonlibrary .org/2009/08/27/wxpython-learning-to-focus/
要点:wx.EVT_KILL_FOCUS 工作正常,但是 wx.EVT_SET_FOCUS 对于任何包含小部件的面板(子项的设置焦点)表现得有点奇怪阻止面板的设置焦点事件按预期触发?)
Interesting article at http://www.blog.pythonlibrary.org/2009/08/27/wxpython-learning-to-focus/
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?)
除了这些人所说的之外,您可能还想尝试 EVT_ENTER_WINDOW 和 EVT_LEAVE_WINDOW。我认为当您将鼠标移入和移出框架小部件时,这些事件就会被触发,尽管我不认为框架必须处于焦点才能触发这些事件。
@休 - 感谢读者!
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!