pyqt multi-window design (2-step implementation, mouth-to-mouth teaching, source code copy available)

This article covers: PyQt5, Qt Designer, PyCharm

 

Table of contents

First look at the finished effect :

Step 1: Create 2 forms with Qt Designer

Step 2: Associate the subform with the button control on the main form

full code


First look at the finished effect: 

        In the video, I only designed 1 main window and 1 sub-window. No matter how many sub-windows you need, the creation method is the same. Let me introduce the specific implementation method.


Step 1: Create 2 forms with Qt Designer

        Open Qt Designer, directly create two Main Window forms at one time, and then I put a button control in the middle of the main form, as shown in the figure below:

         Then save the .ui files of the two forms to the same folder. Note: When saving, you need to position the mouse focus on the form to be saved separately, and save each form separately; and when converting the .ui file into a .py file, you also need to select each .ui separately Files, converted separately, as shown in the following figure after completion:


Step 2: Associate the subform with the button control on the main form

        First look at the untitled2.py file. In the automatically converted code, the object class is inherited by default. The code is as follows:

class Ui_MainWindow(object):

        In order to perform window operations, the inherited object class needs to be modified to the QMainWindow class. Since the QMainWindow class is located in the PyQt5.QtWidgets module, it needs to be imported. The modified code is as follows:

from PyQt5.QtWidgets import QMainWindow

class Ui_MainWindow(QMainWindow):

        After modifying the inheritance class in the .py file of the subform, open the untitled.py main form file. In this file, first define a slot function to use the show() method of the QMainWindow object to open the subform. code show as below:

        

    def open(self):                                # 创建1个自定义函数open打开子窗体
        import untitled2                           # 导入子窗体
        self.second = untitled2.Ui_MainWindow()    # 引用子窗体,并命名为second
        self.second.resize(736, 467)               # 初始化子窗体大小
        self.second.setWindowTitle("这是子窗体")    # 初始化子窗体的标题
        self.second.show()                         # 显示子窗体

        Then associate the clicked() signal of the PushButton button with the custom slot function open(), the code is as follows:

self.pushButton.clicked.connect(self.open)

        Run the untitled.py main form, and click the button to open the subform.


full code

        If you are unclear, you can take a look at the source code below.

        Main form source code (untitled.py):

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(750, 500)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(190, 140, 291, 81))
        self.pushButton.setObjectName("pushButton")

        # 将按钮和显示第2个窗口的自定义函数关联
        self.pushButton.clicked.connect(self.open)

        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "打开新窗口"))

    def open(self):                                     # 创建1个自定义函数open打开子窗体
        import untitled2                                # 导入子窗体
        self.second = untitled2.Ui_MainWindow()         # 引用子窗体,并命名为second
        self.second.resize(736, 467)                    # 初始化子窗体大小
        self.second.setWindowTitle("这是子窗体")         # 初始化子窗体的标题
        self.second.show()                              # 显示子窗体

import sys
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

        Subform source code (untitled2.py):

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow

class Ui_MainWindow(QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(736, 467)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

def __init__(self):
    super(Ui_MainWindow, self).__init__()
    self.setupUi(self)

The weather is cold and the hands are cold, and the code is not easy. I hope everyone who reads the official will click and pay attention. I am grateful~~~

Guess you like

Origin blog.csdn.net/weixin_53989417/article/details/127222824