Skip to content

How to find out which button is clicked when creating multiple buttons in loop? #1169

Closed Answered by falkoschindler
Anindya088 asked this question in Q&A
Discussion options

You must be logged in to vote

Apart from that there's an issue with Python's "late binding":

Try

for i in [1, 2, 3]:
    ui.button(i, on_click=lambda: ui.label(i))

vs.

for i in [1, 2, 3]:
    ui.button(i, on_click=lambda i=i: ui.label(i))

The i=i captures the i within the lambda statement. When the lambda is eventually evaluated, it would use the current value of i (which is now 3). But with i=i the label is created using a local copy.

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@falkoschindler
Comment options

Answer selected by Anindya088
@Anindya088
Comment options

@Baytars
Comment options

@me21
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants