摘要:在软件中,常会有点击某个按钮,显示一个新的子界面的需求,本文介绍如何在PyQt5中实现这一功能,主要涉及知识点是“信号与槽函数的自动绑定”。
程序说明:
1.开发环境:win10系统,pycharm2021,python3.7
2.实现功能:Excel文件合并;
3.界面设计:PyQT5
1.效果图
1.1 父窗口界面
1.2 子窗口界面
二、代码结构
1.主要包含三个文件:父(主)窗口文件、子窗口文件、运行主文件;
2.代码
2.1 main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(621, 337)
Form.setStyleSheet("")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(170, 50, 271, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(170, 150, 271, 51))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "报告自动生成"))
self.pushButton_2.setText(_translate("Form", "报表自动合并"))
2.2 excelMerger.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'excelMerger.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(626, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(280, 20, 101, 31))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(70, 90, 91, 16))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(160, 90, 341, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(510, 90, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(70, 140, 91, 16))
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(160, 140, 171, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(270, 210, 141, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "报表自动合并页面"))
self.label_2.setText(_translate("Dialog", "选择文件路径:"))
self.pushButton.setText(_translate("Dialog", "选择路径"))
self.label_3.setText(_translate("Dialog", "设置文件名称:"))
self.pushButton_2.setText(_translate("Dialog", "点击运行"))
2.3 index.py
import sys
import main as u1
import excelMerger as u2
from PyQt5.QtWidgets import *
class SecondWindow(QMainWindow):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.ui = u2.Ui_Dialog()
self.ui.setupUi(self)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = u1.Ui_Form()
self.ui.setupUi(self)
def on_pushButton_2_clicked(self):
print(123)
win2.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
win2 = SecondWindow()
sys.exit(app.exec_())
3.代码解释
3.1 主窗口文件按钮设置:
代码:
QtCore.QMetaObject.connectSlotsByName(Form)
代码功能:实现对象通过名字来自动绑定槽函数,也就是说如果槽函数的名字与对象某个事件的名字相同,将会实现自动绑定。
3.2 运行文件代码解释:
on_按钮名称_clicked 实现自动绑定。