【问题标题】:Why isActive doesn't work with keybinder?为什么 isActive 不能与键盘绑定器一起使用?
【发布时间】:2012-07-25 13:18:55
【问题描述】:

Why wxframe isn't raised from a function called with global gtk binder? 之后我又遇到了另一个奇怪的行为..

谁能给我一个解决方案(提示也很好:))这个..?

import wx, os
import keybinder

class FrameWithHotKey(wx.Frame):

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        hotkey = "<Ctrl>period"
        keybinder.bind(hotkey, self.toggle_shown)

    def toggle_shown(self):
        # windowNow id
        if self.IsShown():
            self.Hide()
        else:
            self.Show()
            self.Raise()
        print self.IsActive()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = FrameWithHotKey(None)
    app.MainLoop()

如果按下热键 isActive 总是返回 false。为什么?

【问题讨论】:

    标签: window wxpython


    【解决方案1】:

    一些快速的谷歌搜索发现 em this wx.PyApp.IsActive() documentation 指出该方法仅在所讨论的 wx.PyApp 具有焦点时才返回 true。当然,wx.Frame 和 wx.PyApp 是不同的对象。不幸的是,wxpython 的wx.TopLevelWindow documentation 没有提供对IsActive() 的详细解释,但是wxwidget's documentation 似乎表明这种“焦点检查”的推理方式值得追求。

    我采用了您的代码的修改版本(删除了 gtkkeybinder,因为我没有安装它们。将您的 toggle_shown 方法绑定到 wx.EVT_KEY_DOWN)并在 print self.IsActive() 和然后手动运行几行调试代码。我观察到以下行为:

    >self.IsActive()
    假的

    >self.SetFocus()

    >self.IsActive()
    是的

    >self.HasFocus()
    是的

    在我看来,仅仅因为您将窗口放在顶层(使用self.Raise())并不意味着您将应用程序设置为活动的(即赋予它焦点)。

    当然,这引发了一个问题,为什么有两种方法,IsActive()HasFocus(),它们看起来是同一个东西。我不知道这个问题的答案。

    【讨论】:

    • 感谢您的努力,但在我的代码版本(keybinder?)中有所不同。我在 self.Raise() 之后添加了行:self.SetFocus() 并且仍然始终具有来自 IsActive() 的 False 值。
    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多