0

It was hard explaining this in the title. I have 10 buttons that all connect to the same function, but passes different parameters. These parameters are constant, but different for each button.

So I want connections like this

self.btn1.clicked.connect(lambda: self.logic(0))
self.btn2.clicked.connect(lambda: self.logic(1))
self.btn3.clicked.connect(lambda: self.logic(2))

... and so on for my 10 buttons

I tried put all the buttons in a list and used a for-loop like this:

buttons = [self.btn1, self.btn2, self.btn3, ..., self.btn10]
for i in range(0, 10):
    buttons[i].clicked.connect(lambda: self.logic(i))

The problem is that each connection is saved with the variable i itself, not the value contained in the variable. So when pressing the buttons in the program, all of them pass the same parameter which is the last value contained in i (in this case 9)

My question is if it is possible to use the value of a variable as a parameter to make connections?

0 Answers0