Setting click event on dynamic button

Hi,
So I have a situation where I am adding buttons in dynamically, the problem is I cant figure out how to set the “click” event on the dynamically created buttons.
So at the moment I have the buttons appearing but when they are clicked nothing happens.
I have searched and searched and found out how to dynamically add event handlers to them but I cant find out how to dynamically add a raise_event if they are clicked.
I tried calling a function at the time of creation to assign the raise event

for a in matched:
butt = Button(text=a)
self.flow_panel_10.add_component(butt)
def butt_click(self, **event_args):
self.raise_event(‘x-meds’)

but this does nothing.
What am I missing about assigning the click handler to the dynamically created button?
Thanks

2 Likes

You need to set the button’s event handler with something like this:

def butt_click(self, **event_args):
    self.raise_event('x-meds')

for a in matched:
    butt = Button(text=a)
    butt.set_event_handler('click', butt_click)
    self.flow_panel_10.add_component(butt)   

This is described in the docs at https://anvil.works/docs/client/components#events

4 Likes

You Sir are a legend, thanks so much.

3 Likes