关键函数:QPushButton的setObjectName()/objectName()
个人注解:按功能或者区域,将按钮的点击事件绑定的不同的槽函数上。
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_()
时间: 2024-10-05 04:45:07