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

前端 未结 7 1188
北荒
北荒 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:11

    Take advantage of what you can do in a language like Python. You can pass extra arguments to your event callback function, like so.

    import functools
    
    def __init__(self):
        # ...
        for i in range(10):
            name = 'Button %d' % i
            button = wx.Button(parent, -1, name)
            func = functools.partial(self.on_button, name=name)
            button.Bind(wx.EVT_BUTTON, func)
        # ...
    
    def on_button(self, event, name):
        print '%s clicked' % name
    

    Of course, the arguments can be anything you want.

提交回复
热议问题