内置信号和自定义槽使用实例
实现过程同上述步骤一样。槽函数showMsg为自定义函数。
信号与槽:self.pushButton.clicked.connect(self.showMsg)
完整代码如下(可直接拷贝运行,字体加粗部分为添加部分):
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'signal.ui'#
# Created by: PyQt5 UI code generator 5.11.3#
# WARNING! All changes made in this file will be lost!import sysfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBoxclass Ui_Form(object):def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(431, 166)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(160, 50, 91, 41))
font = QtGui.QFont()
font.setFamily("YaHei Consolas Hybrid")
font.setPointSize(14)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "信号与槽"))
self.pushButton.setText(_translate("Form", "运行"))class MyMainForm(QMainWindow, Ui_Form):def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.showMsg)def showMsg(self):
QMessageBox.information(self, "信息提示框", "OK,内置信号与自定义槽函数!")if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())
标签:总结,__,Form,self,showMsg,今日,4.21,font,pushButton From: https://www.cnblogs.com/yangkaiwen/p/17360021.html