[PyQt5-GUI] Build a powerful Python graphical user interface application -- Getting Started Guide

 

Author's homepage: boy who loves to laugh. Blog_CSDN blog - deep learning, activities, python field blogger loves to laugh boy. A boy who is good at deep learning, activities, python, etc., and loves to laugh. Focus on algorithms, python, computer vision, image processing, deep learning, pytorch, neural network, opencv fields. https://blog.csdn.net/Code_and516?type=blog Personal profile: Dagong.

Continue to share: machine learning, deep learning, python-related content, daily BUG solutions, and Windows&Linux practical tips.

If you find an error in the article, please point it out, and I will correct it in time. If you have other needs, you can private message me or send me an email: [email protected] 

        PyQt5 is a powerful Python library for creating graphical user interface (GUI) applications. It is a Python binding based on the Qt framework that allows developers to easily create rich and interactive applications using the Python language. 

This article will take you to the introductory stage of the Python graphical user interface application PyQt5-GUI


Table of contents

1. Looking back on the past

2. Installer

3. Open the program

1. Python direct loading

(1) Find the Python path

(2) Enter Lib

(3) Enter site-packages

(4) Enter qt5_applications

(5) Enter Qt/bin

(6) Double-click to open designer.exe

2. Anaconda3 direct installation

(1) Find the Anaconda3 path and enter envs

(2) Enter the virtual environment you created

(3) Enter Lib

(4) Enter site-packages

(5) Enter qt5_applications

(6) Enter Qt/bin

(7) Double-click to open designer.exe

4. Simple operation

1. Understand the three major blocks of the user interface

2. Create an empty window

3. Change the window name

4. Write a hello world

5. Convert to py source code

1. Click the file path above

2. Enter cmd and press Enter

3. Start conversion

4. Run the source code

6. Example source code

7. Summary


1. Looking back on the past

        In the last chapter "[PyQt5] Building a Powerful Python Graphical User Interface Application--Getting Started Guide" , I have already talked about the introduction, development history, advantages and disadvantages, basic steps and basic use of PyQt5. I believe that everyone has a good understanding of PyQt5 If you have a certain understanding, then this chapter will tell you how to use PyQt5-GUI to perform some simple operations. Just follow my steps step by step to ensure that you have a deeper understanding of PyQt5. Then, let's practice together!

2. Installer

        It is very convenient to install PyQt5-GUI now. It only takes two lines of code to complete the installation. I have to say, Python YYDS~

pip install pyqt5
pip install pyqt5-tools

        It is recommended that you still use the Anaconda3 virtual environment to operate to prevent dependency library conflicts.

        If the network is slow during installation, use the mirror source to download, and the speed will be many times faster.

        It is recommended to use the mirror source of Tsinghua University  https://pypi.tuna.tsinghua.edu.cn/simple/ 

        Below is a screenshot after installation 

        Is the installation procedure very simple? It only takes a few minutes~

        Next, let's start to open the program!

3. Open the program

1. Python direct loading

(1) Find the Python path

(2) Enter Lib

(3) Enter site-packages

(4) Enter qt5_applications

(5) Enter Qt/bin

(6) Double-click to open designer.exe

        Well, the above is the way to open the PyQt5-GUI directly installed in Python. 

2. Anaconda3 direct installation

(1) Find the Anaconda3 path and enter envs

(2) Enter the virtual environment you created

(3) Enter Lib

(4) Enter site-packages

(5) Enter qt5_applications

(6) Enter Qt/bin

(7) Double-click to open designer.exe

        Well, the above is how to open the Anaconda3 virtual environment to install PyQt5-GUI. 

4. Simple operation

1. Understand the three major blocks of the user interface

        Widget Box is a variety of small functional modules we need to use.
        The object viewer is that we can directly call the name of a small module in the code for further operations.
        The property editor is where you can customize various parameters you need.

2. Create an empty window

        Click on the file in the upper left corner

        select new

        Select Main Window

        click create

        Then

        Click on the file in the upper left corner

        Click Save (Ctrl + s)

        Choose your own folder or create a new folder

        It is recommended not to save to the system disk, if there is only one disk, I did not say

        That's it!

        Remember to save every time you add a function or modify it

        You can use the shortcut key Ctrl + r to browse

3. Change the window name

        Find the property editor

        Find the Qwidget

        Find windowTitle and double-click its value to modify it

        Remember to press Enter to save after modification

        Then you can browse with Ctrl + r to view the modified window

4. Write a hello world

        Find Display Widgets under the Widget Box

        Find Label under Display Widgets

        Click and hold the Label with the left mouse button to drag

        Drag it under the window we created

        Then edit hello world

        Ctrl + r to browse

        Remember Ctrl + s to save 

        Just such a small interface is created,

        How about it, isn't it very simple?

        Next, generate the python source code! 

5. Convert to py source code

        If you have reached this point, it means that you have designed an interface,

        But now there are only ui files, no py source code yet,

        So, how to generate it?

        Come and do it with me~

1. Click the file path above

2. Enter cmd and press Enter

        open terminal

        path is the path folder of the ui

3. Start conversion

        First enter the virtual environment (conda activate name) using the Anaconda3 virtual environment

        Then enter the command

pyuic5 -o name.py name.ui

        can be seen

        py file source code already exists

        Don't you think it's done here?

        No no no, one more step

        come on, keep going

4. Run the source code

        First, in the same directory

        Create a main.py file

        write source code in file

        Then just run

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication,QMainWindow

import pyqt5_gui

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = pyqt5_gui.Ui_MainWindow()

    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

        At this point, the entire PyQt5-GUI process is over.

        How about it, isn't it simple?

        You are awesome, I give you a thumbs up~ good~

6. Example source code

        three buttons

        a text box

        interconnected

# -*- coding: utf-8 -*-

import sys
from functools import partial
from PyQt5.QtWidgets import QApplication,QMainWindow

import pyqt5_gui

def button_1(ui):
    print("按钮1")
    ui.textBrowser.append("按钮1")
def button_2(ui):
    print("按钮2")
    ui.textBrowser.append("按钮2")
def button_3(ui):
    print("按钮3")
    ui.textBrowser.append("按钮3")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = pyqt5_gui.Ui_MainWindow()

    ui.setupUi(MainWindow)
    MainWindow.show()
    ui.pushButton.clicked.connect(partial(button_1, ui))
    ui.pushButton_2.clicked.connect(partial(button_2, ui))
    ui.pushButton_3.clicked.connect(partial(button_3, ui))
    sys.exit(app.exec_())

7. Summary

        In this chapter, I explained in detail the installation program of PyQt5-GUI, how to open it, simple operation, how to convert it to source code and simple sample source code. I personally feel that although PyQt5 is not difficult to get started, it will be more complicated when it has more functions, so Be sure to remember the location and function of each module to prevent confusion. Hope this article can help you get started with PyQt5-GUI.
        In short, PyQt5 is a powerful and flexible library. PyQt5-GUI is convenient and fast, and I feel very easy to use. Come and explore more functions of PyQt5 together!

Guess you like

Origin blog.csdn.net/Code_and516/article/details/131589355