Akik Look at that coder

Public account: Look at that code farmer

1. Project Introduction

Build a multi-window GUI demo using PyQt5 based on Python3.x

2. Project preparation

  • Python3.x
  • PyQt5
  • Pycharm

3. Project process

1. Open Qt Designer in Pycharm

2. Use Qt Designer to design GUI Windows

3. Select Main Window and drag QPushButton to create a Ui

4. Save the file to the project directory

5. Repeat the preceding steps to create three UIs

You end up with four UI interfaces

6. Convert UI files into Python code

We get four Python code files for the UI

Use UI 1 as the launch window for the other three UIs

Add the following code to the fir. Py file corresponding to UI 1

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

Working code:

Display result:

7. Association between Windows

Once we’ve created the window we just created, we’ll use UI 1 as the main window and UI 2,3, and 4 as child Windows to call UI 2,3, and 4

Because UI 2,3, and 4 correspond to

  • sec.py,
  • thi.py,
  • fort.py

Automatically converted code inherits from the Object class by default

To perform window operations, you need to change the inherited Object class to the QMainWindows class,

Since the QMainWindows class is in the PyQt5.qtwidGets module,

So there is a need for

  • sec.py
  • thi.py
  • fort.py

, the modified code is

Py,thi.py,fort.py after modifying the inherited classes of sec.py,thi.py,fort.py files,

You need to define a slot function in the fir. Py file to use

QMainWindows object’s show() method,

Use to open UI 2,3, and 4 subwindows

def open(self):
    self.second=sec.Ui_MainWindow()
    self.second.show()
    self.third=thi.Ui_MainWindow()
    self.third.show()
    self.fouth=fort.Ui_MainWindow()
    self.fouth.show()
Copy the code

Also connect the signal slot function in the fir. Py file

self.pushButton.clicked.connect(self.open)
Copy the code

8. Run the program

Click the open button and three new small Windows appear

4. Project thinking

This small project uses PyQt5 to make a simple GUI interface, and at the same time based on Qt signal slot function mechanism to achieve the button signal slot function response, the success of multi-window association.

Suitable for beginners to try to learn PyQt5.

5. Project harvest

  • Novices should note that no number can appear after import, such as import 2, which is incorrect
  • Beginners should note that they need to change the inherited Object class to the QMainWindow class in the child window program.

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign