PyQt5信号和槽 --使用partial为槽函数传递参数
import sys
from functools import partial
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
QLabel, QMainWindow
'''
PyQt5信号和槽 --使用partial为槽函数传递参数
'''
class PartialSlotArgDemo(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("使用partial为槽函数传递参数")
button1 = QPushButton("按钮1")
button2 = QPushButton("按钮2")
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
mainFrame = QWidget()
mainFrame.setLayout(layout)
self.setCentralWidget(mainFrame)
a = 19
# button1.clicked.connect(lambda :self.onButtonClick(10,20))
# button2.clicked.connect(lambda :self.onButtonClick(10,a))
# button1.clicked.connect(lambda :QMessageBox.information(self,'结果','单击了button1'))
button1.clicked.connect(partial(self.onButtonClick,10,30))
button2.clicked.connect(partial(self.onButtonClick,a,30))
def onButtonClick(self,m,n):
print("m+n:",m+n)
QMessageBox.information(self,'结果',str(m+n))
if __name__ == '__main__':
app = QApplication(sys.argv)
win = PartialSlotArgDemo()
win.setGeometry(400,200,500,200)
win.show()
sys.exit(app.exec_())