Get components by tag?

What I’m trying to do:

Get a component by its tag, for eg.
event_args[‘sender’].parent.get_by_tag(“imagefile”)

What I’ve tried and what’s not working:

I looked through the docs but cannot find a way to get a specific component by tag, other than iterating all a containers get_components() children and searching for the component I want.

Code Sample:

event_args['sender'].parent.get_by_tag("imagefile")

It depends how you’ve defined the component within the parent form.

It’s not really a pattern used in anvil since you probably already have a reference to the component from the design view e.g. parent.image_1 or you can create a reference in code.

Do you have a larger code snippet or a clone link you can share? It’ll be easier to see what you’re trying to do.

I’m creating a new form dynamically based off answers in another form. In the dynamic form, I make many LinearPanels, each has one or more FlowPanels. Each flow panel has a textbox and some buttons. Added FlowPanels can be created on the form dynamically. So I don’t quite have references to all these on the main Form. If you click a button in the FlowPanel, then it knows that parent is a FlowPanel containing the textbox etc. I want the event handler for the button to find the textbox using something like parent.get_by_tag(“answertextbox”) etc.

There’s plenty of ways to do this. It just depends how you’re dynamically generating the code. If you have some more snippets it would be handy…

Here’s are some examples that might help


lp = LinearPanel()
tb = TextBox()
btn = Button()
btn.set_event_handler('click', lambda **event_args: print(tb))

lp.add_component(tb)
lp.add_component(btn)

Or if you want to stick down the parent root

def btn_click(**event_args):
    print(event_args['sender'].parent.tag.answertextbox)

lp = LinearPanel()
tb = TextBox()
lp.tag.answertextbox = tb
btn = Button()
btn.set_event_handler('click', btn_click)

lp.add_component(tb)
lp.add_component(btn)

Maybe that’s not relevant but it’s difficult to give specific advice without more code.

1 Like
lp.tag.answertextbox = tb

Ah I didn’t think about this. Yep this is exactly what I want. Thanks!

1 Like