Best way to get the name of a button that called an event?

前端 未结 7 1175
北荒
北荒 2021-01-03 05:26

In the following code (inspired by this snippet), I use a single event handler buttonClick to change the title of the window. Currently, I need to evaluate if t

相关标签:
7条回答
  • 2021-01-03 06:12

    I recommend that you use different event handlers to handle events from each button. If there is a lot of commonality, you can combine that into a function which returns a function with the specific behavior you want, for instance:

    def goingTo(self, where):
        def goingToHandler(event):
            self.SetTitle("I'm going to " + where)
        return goingToHandler
    
    def __init__(self):
        buttonA.Bind(wx.EVT_BUTTON, self.goingTo("work"))
        # clicking will say "I'm going to work"
        buttonB.Bind(wx.EVT_BUTTON, self.goingTo("home"))
        # clicking will say "I'm going to home"
    
    0 讨论(0)
提交回复
热议问题