I'm populating some cards using for-loop in NiceGUI.Each has a label and a button.Trying to access label id but it prints last label id only on click

huangapple go评论81阅读模式
英文:

I'm populating some cards using for-loop in NiceGUI.Each has a label and a button.Trying to access label id but it prints last label id only on click

问题

点击按钮时,应该只打印卡片内部标签的ID。但无论我点击哪个按钮,它总是打印出最后一个标签的ID。

在下面的代码中,my_grid是我试图填充卡片的网格的名称,listcards是我试图放入卡片中的卡片标签的列表。

with my_grid:
   for i in range(0,len(listcards)):
       with ui.card() as mycard:
           label_card = ui.label(text=f"{listcards[i]}")
           bt = ui.button("ID", on_click=lambda:print(label_card.id))

当我点击按钮时,我想要打印出那张卡片的标签ID,但它总是打印出最后一个标签的ID。我真的想解决这个问题。有什么建议吗?

英文:

When I click on the button it should print the id of the label inside that card only. But it always prints the id of last label no matter which button I click on.

In the code below my_grid is the name of the grid in which I'm trying to populate the cards and listcards is the list of cards labels that I'm trying to put inside the card

Code

with my_grid:   
   for i in range(0,len(listcards)):
       with ui.card() as mycard:
           label_card = ui.label(text=f"{listcards[i]}")
           bt = ui.button("ID", on_click=lambda:print(label_card.id))

When I click on the button I want to print the label id of that card only but it always prints the id of last label. I really want to fix this issue. Any suggestions?

答案1

得分: 0

这是一个非常经常被问到的问题。实际上,由于这个问题,我们正在考虑引入一个FAQ部分。但这确实是一个令开发人员感到困惑的棘手的Python行为。

在以下代码示例中,按钮点击将调用lambda函数,lambda函数然后打印label_card.id的值。由于for循环在按钮点击之前已经终止,所以label_card是带有文本"9"的标签。因此,无论点击哪个按钮,都会打印出最后一个标签的ID。

for i in range(10):
    with ui.card():
        label_card = ui.label(text=i)
        ui.button("ID", on_click=lambda: print(label_card.id))

诀窍是将label_card=label_card添加到lambda函数的参数列表中。lambda函数对于每个循环周期都是唯一的。它的参数也是唯一的。label_card变量的当前值被写入唯一lambda函数的参数列表中。这样,按钮点击将打印出_对应的_label_card的ID。

for i in range(10):
    with ui.card():
        label_card = ui.label(text=i)
        ui.button("ID", on_click=lambda label_card=label_card: print(label_card.id))
英文:

This is a very frequently asked question. We're actually thinking about introducing a FAQ section because of this question. But it is indeed a tricky Python behavior that keeps confusing developers.

In the following code example, a button click will call the lambda function, which in turn prints the value of label_card.id. Since the for loop has terminated long before the button click, label_card is the label with text "9". Therefore, regardless of which button is clicked, the ID of this last label is printed.

for i in range(10):
    with ui.card():
        label_card = ui.label(text=i)
        ui.button("ID", on_click=lambda: print(label_card.id))

The trick is to add label_card=label_card to the parameter list of the lambda function. The lambda function is unique for every loop cycle. And so are its parameters. The current value of the label_card variable is written into the parameter list of the unique lambda function. This way a button click prints the ID of the corresponding label_card.

for i in range(10):
    with ui.card():
        label_card = ui.label(text=i)
        ui.button("ID", on_click=lambda label_card=label_card: print(label_card.id))

huangapple
  • 本文由 发表于 2023年7月20日 12:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定