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

前端 未结 7 1199
北荒
北荒 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 05:48

    I needed to do the same thing to keep track of button-presses . I used a lambda function to bind to the event . That way I could pass in the entire button object to the event handler function to manipulate accordingly.

        class PlatGridderTop(wx.Frame):
            numbuttons = 0
            buttonlist = []
    
    
            def create_another_button(self, event): # wxGlade: PlateGridderTop.
                    buttoncreator_id = wx.ID_ANY
                    butonname = "button" + str(buttoncreator_id)
                    PlateGridderTop.numbuttons = PlateGridderTop.numbuttons + 1
                    thisbutton_number = PlateGridderTop.numbuttons
    
                    self.buttonname  =  wx.Button(self,buttoncreator_id ,"ChildButton %s" % thisbutton_number )
                    self.Bind(wx.EVT_BUTTON,lambda event, buttonpressed=self.buttonname: self.print_button_press(event,buttonpressed),self.buttonname)
                    self.buttonlist.append(self.buttonname)
                    self.__do_layout()
                    print "Clicked plate button %s" % butonname
                    event.Skip()
           def print_button_press(self,event,clickerbutton):
                   """Just a dummy method that responds to a button press"""
                   print "Clicked a created button named %s with wxpython ID %s" % (clickerbutton.GetLabel(),event.GetId())
    

    Disclaimer : This is my first post to stackoverflow

提交回复
热议问题