This question has been asked a thousand times, and I do apologize for the repetition however it's evident to me I am missing a fundamental piece of this puzzle.
I've got a pretty standard iterative qtbutton setup:
for i, button in enumerate(qtbuttons):
button.clicked.connect(lambda i=i: doTheThing(i))
def doTheThing(index):
print("Button pushed has index %s"%index)
My understanding was that by doing lambda i=i:
, the lambda would retain the index at the time it was assigned, thereby preventing all buttons from returning the last index. What I ended up with was the exact opposite issue. All buttons now return "0".
I also tried string formatting, something pointed to me in another thread, which did not work either.
I was working off of the info in this thread Python's lambda iteration not working as intended which is now 10 years old.
If possible, I'd really like to know:
- Why this is returning "0" for each button.
- How to achieve the desired behavior.