wxpython确定鼠标或键是否触发了设置焦点事件

时间:2011-01-24 20:42:13

标签: wxpython

是否可以在wxpython中确定小部件如何获得焦点? 即通过鼠标点击或Tab键或箭头键等 谢谢:))

1 个答案:

答案 0 :(得分:0)

我玩过这个,发现EVT_SET_FOCUS只携带有关剩下的窗口的信息。所以你真的想要绑定EVT_LEFT_DOWN。

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.last_was_mouse = False

        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel, label="Test")
        self.button2 = wx.Button(self.panel, label="Test")
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.button)
        self.sizer.Add(self.button2)
        self.panel.SetSizerAndFit(self.sizer)
        self.Show()

        self.button2.Bind(wx.EVT_SET_FOCUS, self.OnFocus)
        self.button2.Bind(wx.EVT_LEFT_DOWN, self.OnMouse)


    def OnFocus(self, e):
        if self.last_was_mouse:
            print "Focus by mouse"
        else:
            print "Focus by keyboard"
        self.last_was_mouse = False

    def OnMouse(self, e):
        self.last_was_mouse = True
        e.Skip()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()