PyQt5 implements clicking the button in the parent window to display the child window (window nesting function)

Abstract: In software, there is often a need to click a button to display a new sub-interface. This article introduces how to implement this function in PyQt5. The main knowledge point is “automatic binding of signal and slot functions “.

Program Description:

1. Development environment: win10 system, pycharm2021, python3.7

2. Realized function: Excel file merge;

3. Interface design: PyQT5

1. Effect picture

1.1 parent window interface

1.2 Sub-window interface

2. Code structure

1. Mainly contains three files: parent (main) window file, child window file, running main file;

2. Code

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", "Report is automatically generated"))
        self.pushButton_2.setText(_translate("Form", "Reports are automatically merged"))

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", "The report automatically merges the page"))
        self.label_2.setText(_translate("Dialog", "Select file path:"))
        self.pushButton.setText(_translate("Dialog", "Select Path"))
        self.label_3.setText(_translate("Dialog", "Set file name:"))
        self.pushButton_2.setText(_translate("Dialog", "Click to run"))

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. Code explanation

3.1 Main window file button settings:

code:

QtCore.QMetaObject.connectSlotsByName(Form)

Code function: Realize that the object automatically binds the slot function by name, that is to say, if the name of the slot function is the same as the name of an event of the object, automatic binding will be realized.

3.2 Run file code explanation:

on_button_name_clicked for automatic binding.