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

前端 未结 7 1178
北荒
北荒 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:07

    You could create a dictionary of buttons, and do the look based on the id ... something like this:

    class MyFrame(wx.Frame):
       def _add_button (self, *args):
          btn = wx.Button (*args)
          btn.Bind (wx.EVT_BUTTON, self.buttonClick)
          self.buttons[btn.id] = btn
       def __init__ (self):
          self.button = dict ()
          self._add_button (self.panel1, id=-1,
            pos=(10, 20), size = (20,20))
    
        self._add_button = (self.panel1, id=-1,
            pos=(40, 20), size = (20,20))
    
        self.Show (True)
    
       def buttonClick(self,event):
          self.SetTitle (self.buttons[event.Id].label)
    

提交回复
热议问题