wxPython 中 parent.Bind 和 widget.Bind 有什么区别

What is the difference between parent.Bind and widget.Bind in wxPython

import wx

class MyPanel(wx.Panel):

    def __init__(self, parent):
        super().__init__(parent)
        btn = wx.Button(self, label="Press me")
        btn.Bind(wx.EVT_BUTTON, self.on_button_press)

    def on_button_press(self, event):
        print("You pressed the button")

class MyFrame(wx.Frame):

    def __init__(self):
        super().__init__(parent=None, title="Hello wxPython")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = MyFrame()
    app.MainLoop()

在上面的代码中,我们使用 btn.Bind 将 wx.Button 绑定到 wx.EVT_BUTTON。
如果相反,我们使用这种方式: self.Bind(wx.EVT_BUTTON, self.on_button_press, btn)
结果将与上述相同。现在我的问题是 self.Bind 和 btn.Bind 之间的区别。

每个小部件都有一个 Id。
触发事件时,传递触发小部件的 ID,在本例中为按钮。
将事件绑定到函数可以是特定的或通用的,即特定的小部件或触发该事件类型的任何小部件。
简而言之,在这种情况下,self.Bind 绑定任何按钮事件,除非您指定小部件 ID。
参见:https://docs.wxpython.org/events_overview.html
希望下面的代码有助于解释。
N.B。 event.Skip() 表示不要在此事件停止,看看是否还有更多事件要处理。

import wx

class MyPanel(wx.Panel):

    def __init__(self, parent):
        super().__init__(parent)
        btn1 = wx.Button(self, label="Press me 1", pos=(10,10))
        btn2 = wx.Button(self, label="Press me 2", pos=(10,50))
        Abtn = wx.Button(self, label="Press me", pos=(10,90))

    # Bind btn1 to a specific callback routine
        btn1.Bind(wx.EVT_BUTTON, self.on_button1_press)
    # Bind btn2 to a specific callback routine specifying its Id
    # Note the order of precedence in the callback routines
        self.Bind(wx.EVT_BUTTON, self.on_button2_press, btn2)
    # or identify the widget via its number
    #    self.Bind(wx.EVT_BUTTON, self.on_button2_press, id=btn2.GetId())
    # Bind any button event to a callback routine
        self.Bind(wx.EVT_BUTTON, self.on_a_button_press)

    # button 1 pressed
    def on_button1_press(self, event):
        print("You pressed button 1")
        event.Skip()

    # button  2 pressed
    def on_button2_press(self, event):
        print("You pressed button 2")
        event.Skip()

    # Any button pressed
    def on_a_button_press(self, event):
        print("You pressed a button")
        event.Skip()

class MyFrame(wx.Frame):

    def __init__(self):
        super().__init__(parent=None, title="Hello wxPython")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = MyFrame()
    app.MainLoop()