Add events to buttons in Linear Panel

What I’m trying to do: I am trying to develop a loop so that values from a range or list will be automatically added to the liner panel. Like

for i in range(1,200):
    self.linearpanel.addcomponents(Button(text=i))

(Don’t mind the syntax)

However, the problem is that I can’t assign anything to them. What I want to do is that I want users to be able to click a button from linear panel and it will set a global variable to the particular text on that button.

If that is not possible, can anyone teach me how to add elements from a list into dropdown??

When you say you can’t add anything to them - that’s because you haven’t kept a reference to them inside the loop.


for i in range(1,200):
    b = Button(text=i, tag=i)
    self.linearpanel.add_component(b)
    b.set_event_handler('click', self.click_handler)
    self.buttons[i] = b

By assiging the button to a variable you can then attach a handler or keep a global reference to that button

Click handlers also have an event_arg sender which refers to the button that was clicked.

def click_handler(self, **event_args):
    sender = event_args['sender']
    sender.text = 'I was just clicked'
    print(sender.tag, 'was clicked')

Forgive me but I didn’t understand the command properly.

Suppose I have a list
l=[‘a’,‘b’,‘c’,‘d’,‘e’]

Now I want to add all elements to the linear panel so that when I click on any one of them(let’s take ‘c’ for example), it will lead me to a new form and display the text ‘c’ there.

Just like that, if I click on ‘d’, it will display the text ‘d’ in the new form.

Thanks again for your quick response

Here’s a clone link to demonstrate what I mean

https://anvil.works/build#clone:XDMKJ4KW3JHQ5HQF=KG5BONZWW77ABVZIW42S6CZ2

1 Like

Once again you have my thanks. It worked perfectly

1 Like