0

I'm creating a bunch of QPushButton's in PyQt6 dynamically in a loop. They all use the same target function and the only difference between them is one argument they are receiving, but for some reason, when i assign them dynamically, the all get the arg of the last one assigned.

Doing this:

for i in range(n):
    btn.clicked.connect(lambda: click_event(i))

All events get assigned like this, with n as the argument:

btn.clicked.connect(lambda: click_event(n))
btn.clicked.connect(lambda: click_event(n))
btn.clicked.connect(lambda: click_event(n))
...

But if i assign them "manually":

btn.clicked.connect(lambda: click_event(0))
btn.clicked.connect(lambda: click_event(1))
btn.clicked.connect(lambda: click_event(2))
...

They work flawless..

Any idea what could it be?

Ghost
  • 1,426
  • 5
  • 19
  • 38

0 Answers0