PyQt5 learning notes - program structure analysis

Posted by phpscriptcoder on Sat, 15 Jan 2022 11:59:41 +0100

1, Briefly introduce some controls

Official document: '‘ ’’’

  • QtWidgets: contains a complete set of UI element controls, which are used to establish an interface in line with the system style
  • QtGui: covers a variety of basic graphics functions, such as font, graphics, icon and color
  • QtCore: covers the core non GUI functions of the package, such as time, file, directory, data type, link, thread and process
  • QtWebKit: browser engine
  • QtTest: Test
  • QtSql: Database
  • QtMultimedia and QtMultimedia widgets: Multimedia
  • Qt: integrate the classes in basically all modules into a single module. The advantage is that you don't care which module contains which classes. The disadvantage is that it occupies memory

2, Program simple analysis

from PyQt5.QtWidgets import QLabel,QPushButton
from PyQt5.QtGui import QIcon

from PyQt5.Qt import *
import sys

# Create an application object and pass in parameters
app = QApplication(sys.argv)

# Create control
# When we create a control, if the control has no parent control, we treat it as a top-level control (window)
# The system will automatically add some decoration (title bar) to the window, and the window control has some characteristics (setting title and icon)

window = QWidget()

# Set control (size, position, style...)
window.setWindowTitle("Society is my brother, I don't talk much") # The title bar of the parent control set must be a top-level control

# Set size

# Create a QLabel control as a child control, and the parent control can be used as a container for the child control
# If the Label here is a child control, you cannot set the title setWindowTitle()
label = QLabel(window)  # Add parent control
label.setText("Hello world")

# Move label

# Display control. After a control has just been created (this control has no parent control), it will not be displayed by default. It can only be displayed by manually calling show()
# If this control has a parent control, in general, after the parent control is displayed, the child control will be displayed automatically

# The execution of the application enters the message loop
# Let the whole program begin to execute and enter the message loop
# Monitor the user interaction information received by the whole program

design sketch:

Structure diagram of the program:


  • A PyQt program requires an application object that contains a main event loop in which all time from the window system and other resources is processed and scheduled.
  • It also handles the initialization and termination of applications and provides dialog management
  • He also handles most system wide settings
  • app.exec() means to let the program enter the main loop without stopping
  • A control without a parent object is not displayed by default. You must call show()
  • Multiple top-level controls can be displayed in an application:
    • If a Widget has no parent control, it is considered a top-level control (top-level window)
    • If you want a control to be displayed inside another control, you must have a parent-child relationship
    • If the two objects are parent-child, the general child objects will be displayed automatically after the parent object is displayed

If a control has no parent control, you can set the title separately:

label = QLabel()
label.setText("XXX")  # Set the text content for the label
label.setWindowTitle("xxxxxxxx") # Set title

3, Activity template settings

Open settings, enter live, select Live Templates, and then click the plus sign on the right to select Live Templates

4, Object oriented version

If all control operations are packaged into one class, the code is defined as follows:

# Create a window class and inherit QWidget (itself is a top-level class)
class Window(QWidget):
    # Initialization method
    def __init__(self):

        # Looking at the definition of QWidget, you can see that its initialization has many parameters
        # Here, you need to initialize the QWidget and call the super method before initializing the Window subclass


# Qt contains some basic modules
from PyQt5.Qt import *
import sys

# Execution method: right click to run the command line: python code name
# When others start the program through the command line, they can set a function (accept the parameters passed by the command line to execute different logic, that is, there are several if else in the code, and use 0 1 2 3 to select which code block to execute)
# The parameter 0 represents the path of the current project

class Window(QWidget):
    def __init__(self):
        self.setWindowTitle("study PyQt")

        label = QLabel(self)

app = QApplication(sys.argv)
window = Window()

# window.setWindowTitle("society is my brother, and people don't talk much")
# window.resize(500,500)

# label = QLabel(window)
# label.setText("Hello world")
# label.move(200,200)

However, in reality, there are many sub controls in the development work, and it is impossible to add all the child controls to the init() method. Therefore, the following changes can be made. The code structure is clearer, each sub control is used as a single method, while the init() method only keeps the top-level control QWidget(), so it is necessary to call other controls in the init() method.

class Window(QWidget):
    def __init__(self):

        # Call the initialization method of the parent QWidget

        # Set the overall window control, top-level control
        self.setWindowTitle("PyQt5 Study notes")

        # Then you call the functions of some child controls

    def setup_ui(self):
        label = QLabel(self)

You can use the encapsulated class as a separate class, and then use the file as an import module, which is very maintainable. At the same time, you can add the following code under this module to facilitate testing

if __name__ == '__main__':
    import sys  # Import system module
     app = QApplication(sys.argv)
     window = Window()

Topics: Qt UI PyQt5