1, Briefly introduce some controls
Official document: '‘ https://doc.qt.io/qtforpython/modules.html ’’’
- 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 window.resize(500,500) window.move(400,200) # 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 label.move(200,200) # 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 window.show() # 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 sys.exit(app.exec_())
design sketch:
Structure diagram of the program:
Summary:
- 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 label.show()
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 super().__init__() print("xxx")
# 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): super().__init__() self.setWindowTitle("study PyQt") self.resize(500,500) label = QLabel(self) label.setText("xxx") 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) window.show() sys.exit(app.exec_())
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 super().__init() # Set the overall window control, top-level control self.setWindowTitle("PyQt5 Study notes") self.resize(500,500) # Then you call the functions of some child controls self.setup_ui() def setup_ui(self): label = QLabel(self) label.setText("xxx")
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() window.show() sys.exit(app.exec_())