How do I assert the identity of a PyQt5 signal?

后端 未结 2 1889
无人及你
无人及你 2021-01-23 12:14

I\'m in a situation where I want to assert the identity of a PyQt5 signal. Specifically, I want to check whether the clicked signal from a given QPushButton

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-23 12:41

    Try it:

    import sys
    from PyQt5.QtWidgets import QApplication, QPushButton
    
    app = QApplication([])
    
    def clickButton(w):
        # Check what you clicked here.                        # <-----
        return print(w.text())
    
    widget  = QPushButton('Button 1')
    widget.clicked.connect(lambda : clickButton(widget))
    
    widget2 = QPushButton('Button 2')
    widget2.clicked.connect(lambda : clickButton(widget2))
    
    widget.setGeometry(300, 150, 100, 100)
    widget.show()
    
    widget2.setGeometry(450, 150, 100, 100)
    widget2.show()
    
    sys.exit(app.exec_())
    

提交回复
热议问题