PyQt5小笔记
头部加
import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.Qt import * class MyWindow(QtWidgets.QMainWindow): my_button = pyqtSignal() def __init__(self,parent=None,*args,**kwargs): super(MyWindow,self).__init__(parent,*args,**kwargs) self.setupUi(self)
尾部加
if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() sys.exit(app.exec_())
槽函数改:把Form改成self
self.pushButton.clicked.connect(self.my_def)
信号是:
my_button = pyqtSignal()
内部槽函数写
self.my_button.emit()
整体:
# -*- coding: utf-8 -*- import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.Qt import * class MyWindow(QtWidgets.QMainWindow): my_button = pyqtSignal() def __init__(self,parent=None,*args,**kwargs): super(MyWindow,self).__init__(parent,*args,**kwargs) self.setupUi(self) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(150, 120, 101, 41)) self.pushButton.setObjectName("pushButton") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(140, 40, 191, 41)) self.label.setObjectName("label") self.retranslateUi(Form) self.pushButton.clicked.connect(self.my_def) QtCore.QMetaObject.connectSlotsByName(Form) def my_def(self): self.my_button.emit() def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "切换")) self.label.setText(_translate("Form", "我是第一个界面")) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() sys.exit(app.exec_())
外部就是:
# *_*coding:utf-8 *_* # @Author : zyb import one from PyQt5.Qt import * if __name__ == '__main__': import sys app = QApplication(sys.argv) window = one.MyWindow() def my_def_w(): print(999) window.my_button.connect(my_def_w) window.show() sys.exit(app.exec_())
主界面的菜单栏操作--退出
区别就是clicked换成triggered
self.index_close_b.triggered.connect(self.index_close)