Using lambda expression to connect slots in pyqt

前端 未结 3 1217
清酒与你
清酒与你 2020-11-22 12:06

I am trying to connect slots with lambda functions, but it\'s not working the way I expect. In the code below, I succeed in connecting the first two buttons correctly. For t

3条回答
  •  鱼传尺愫
    2020-11-22 12:56

    The QPushButton.clicked signal emits an argument that indicates the state of the button. When you connect to your lambda slot, the optional argument you assign idx to is being overwritten by the state of the button.

    Instead, make your connection as

    button.clicked.connect(lambda state, x=idx: self.button_pushed(x))
    

    This way the button state is ignored and the correct value is passed to your method.

提交回复
热议问题