pyqt5—多按钮的槽链接到一个函数

转载自:https://www.cnblogs.com/ribavnu/p/4672880.html
请读原文
代码可直接运行,关键就是main.sender()这里,注意修改main

from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QHBoxLayout,
                                        QLineEdit)
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys 
 
 
def clickHandle():
    ##main代表按钮的容器widget的对象
    button = main.sender()
    print(button.objectName())
 
 
if __name__ == '__main__':                  
    app=QApplication(sys.argv) 
    main=QWidget() 
     
    latout = QHBoxLayout()
    main.setLayout(latout)
     
    but_1 = QPushButton('but_1')
    but_1.setObjectName('but_1')
    but_1.clicked.connect(clickHandle)
    but_2 = QPushButton('but_2')
    but_2.setObjectName('but_2')
    but_2.clicked.connect(clickHandle)
     
    edit_1 = QLineEdit()
    edit_2 = QLineEdit()
     
    latout.addWidget(but_1)
    latout.addWidget(but_2)
     
    latout.addWidget(edit_1)
    latout.addWidget(edit_2)
     
    main.show() 
    app.exec_()

你可能感兴趣的:(深度学习部署)