Connecting slots and events in PyQt4 in a loop
- by LukaD
Im trying to build a calculator with PyQt4 and connecting the 'clicked()' signals from the buttons doesn't as expected.
Im creating my buttons for the numbers inside a for loop where i try to connect them afterwards.
def __init__(self):
        for i in range(0,10):
            self._numberButtons += [QPushButton(str(i), self)]
            self.connect(self._numberButtons[i], SIGNAL('clicked()'),
                    lambda : self._number(i))
    def _number(self, x):
        print(x)
When I click on the buttons all of them print out '9'.
Why is that so and how can i fix this?