python GUI programming PyQT5

1: Introduction to GUI development                 

GUI is the abbreviation of Graphical User Interface (Graphical User Interface)

In the GUI, not just typing text and returning text, users can see windows, buttons, text boxes and other graphics, and they can click with the mouse and type through the keyboard.

A GUI is a different way of interacting with a program.

The programs so far have been command-line or text-mode programs

GUI programs still have 3 basic elements: input, processing and output

Two: Introduction to GUI framework under PYTHON

Three: PYQT installation and use

The first step: install python

Step 2: Install PyQt5-sip

sip is a Python/C++ hybrid programming solution for PyQt developed by RiverBank (the developer of PyQt). PyQt as a Python Module
depends on sip as a Python Module.

Step 3: Install PyQt5

 Test whether the installation is successful

Then create a new .py file in any directory, and enter the following code in the file

 Then save, run, and a window appears, indicating success 

Four: PyQT module introduction

Five: PyQT main class introduction

The PyQt5 API has more than 620 classes and 6000 functions. It is a cross-platform toolkit that can run on all major operating systems, including Windows, Linux and Mac OS

QObject class: In the class hierarchy is the top class (Top Class), which is the base class of all PyQt objects.

QPaintDevice class: the base class for all drawable objects.

QApplication class: used to manage the control flow and main settings of GUI applications. It contains the main event loop, which handles and dispatches all events from the window system and other resources; it also handles application initialization and termination, and provides dialog management; it also handles most system-wide and application-wide Settings are processed.

QWidget class: the base class for all user interface objects. The QDialog class and the QFrame class inherit from the QWidget class, and these two classes have their own sub-class system (Sub-Class System).

QFrame class: The base class for window controls with frames. It is also used to directly create a simple frame without any content, but QHBox or QVBox is usually used because they can automatically arrange the window controls placed in the frame.

QMainWindow class: Provides a main application window with a menu bar, docking windows (such as toolbars) and a status bar.

QDialog class: the most common top-level window. If a widget is not embedded in a parent widget, then the widget is called a top-level widget. Typically, a top-level window control is a window with a frame and title bar. In Qt, QMainWindow and various QDialog subclasses are the most common top-level windows

QWidget
Is the base class of all user interface classes, it can receive all mouse, keyboard and other system window events. A Widget that is not embedded in the parent window will be called as a window. Of course, it can also use the setWindowFlags(Qt.WindowFlags) function to set the display effect of the window.
The constructor of QWidget can receive two parameters, the first parameter is the parent window of the window; the second parameter is the Flag of the window, which is Qt.WindowFlags. Determine whether the Widget is embedded in the parent window or called as an independent window according to the parent window, and set some properties of the Widget window according to the Flag.
QMainWindow
(Main window) is generally the framework of the application program, and you can add required Widgets in the main window, such as adding menu bar, toolbar, status bar, etc.
The main window is usually used to provide a large central window controls (such as text editing or drawing canvas) and surrounding menu bar, toolbar and status bar.
QMainWindow is often inherited, which makes it easier to encapsulate central controls, menu bars, toolbars, and window states. It is also possible to use Qt Designer to create the main window.
QApplication
The QApplication class is used to manage the control flow and main settings of the graphical user interface application. It can be said that QApplication is the lifeblood of PyQt's entire background management. Any graphical user interface application developed using PyQt has a QApplication object.
The initialization of the QApplication class is included in the PyQt application instance, which is usually placed after the if name == "main": statement of the Python script, similar to the main function of C, as the entry of the main program. Because the QApplication object does a lot of initialization, it must be created before creating the window.
ØThe QApplication class can also handle command line parameters. When the QApplication class is initialized, the parameter sys.argv needs to be introduced. sys.argv is a list of parameters from the command line, and Python scripts can be run from the shell, such as double-clicking qtSample.py with the mouse to start a PyQt application. After introducing sys.argv, the program can be started from the command line. For example, entering python qtSample.py on the command line can also achieve the same effect.
QApplication adopts the event loop mechanism. When QApplication is initialized, it enters the main loop (Main Loop) of the application and starts event processing. The main loop receives events from the window system and distributes these events to the controls of the application. The main loop ends when the sys.exit() function is called.
PyQt 5 applications are event-driven, such as keyboard events, mouse events, etc. In the absence of any events, the application is sleeping. The main loop controls when the application goes to sleep and when it is woken up.

Six: What is layout management

Layout management is an important aspect in GUI programming. Layout management is a method of how to place components on the application window
We can manage layouts in two basic ways. We can use absolute positioning and layout classes.

Seven: layout management

Eight: Horizontal layout

Nine: Vertical layout

Ten: grid layout

Eleven: Form layout

Twelve: absolute positioning

The program specifies the position of the components and the size of each component measured in pixels. When you use absolute positioning, we need to be aware of the following limitations:

If we change the window size, the position and size of the component will not change.

Apps may look different on different platforms

Changing the fonts in our application can mess up the application.

If we decide to change our layout, we have to completely rewrite our layout, which is very tedious and a waste of time.

Thirteen: Catalog

1. Control classification

  1. Window class 2. Layout control 3. Advanced interface control

2. Basic controls

  1. Label control 2. Text box control 3. Button control

  4. Drop-down list box 5. Table control 6. Time control

3. What are signals and slots

4. Built-in signal and slot functions

5. Built-in signals and custom slot functions

6. Custom signals and built-in slot functions

7. Custom signal and slot functions

Fourteen: window class

If it is the main window, use the QmainWindow class;

If it is a dialog box, use the Qdialog class;

If you are not sure, or it may be a top-level window, or it may be embedded in other windows, then use the Qwidget class.

The difference between QWidget, QMainWindow, QDialog and QFrame, especially the difference between QWidget and QFrame

    All classes in PyQt inherit from the QObject class. QWidget directly inherits from the QPaintDevice class, and QDialog,
QMainWindow, and QFrame directly inherit from the QWidget class. The QWidget class is the base class for all user interface objects.
The QDialog class is the base class for dialog windows. The QFrame class is the base class for widgets with frames, such as:
QPushButton, QLabel... ---> QFrame ---> QWidget.

(1) QmainWindow: main window class
Usually used to create the main window, it inherits from the Qwidget class and has its derived methods and properties.
(2) QWidget: control class
It is the base class of all user interface objects, and all windows and controls inherit directly or indirectly from the QWidget class.
(3) Qdialog: dialog window class
QMessageBox : message popup dialog box
QInputDialog : input dialog
Inheritance relationship: QMessageBox, QFileDialog, QFontDialog, QInputDialog --->Qdialog

Fifteen: Layout Controls

(1) QBoxLayout: box layout
Inheritance relationship: QHBoxLayout, QVBoxLayout ---> QBoxLayout ---> QLayout ---> Qobject
1. QHBoxLayout: Horizontal layout adds controls in order from left to right
2. QVBoxLayout: Vertical layout adds controls in order from top to bottom
(2) QGridLayout: The grid layout divides the windows into a grid of rows and columns for arrangement.
(3) QFormLayout: form layout
Inheritance relationship: QFormLayout ---> QLayout ---> QObject realizes the layout of the form, which consists of two columns. The first column is used to display information and give user prompts. It is generally called the label field; the second column requires the user to select or Input, generally called field domain. .
(4) Qsplitter
You can dynamically drag the boundaries between sub-controls to control the size of sub-controls. It is a dynamic layout manager
There are 4 layout methods in PyQt5: horizontal layout, vertical layout, grid layout, form layout
There are 2 layout methods in PyQt5: addLayout(), addWidget()

 Sixteen: Advanced Interface Controls

(1) Table and tree:
QTableView
The QTableView control can bind a model data to update the content on the control
QListView
The QListView class is used to display data
QListWidget
is an entry-based interface for adding or removing entries from a list. Each entry object is a QListWidgetItem object
QTableWidget
It is a space commonly used to display data tables in Qt programs
QTreeView
The QTreeView class realizes the tree structure, and the tree structure is realized through the QTreeWidgetQ and TreeWidgetItem classes, among which the TreeWidgetItem class realizes the addition of tree nodes
(2) Container: load more controls
It is used to solve the problem that a window cannot be loaded or too many controls are loaded and it is not beautiful
QTabWidget: tab control
Provides a tab and a page area, the first tab is displayed by default
QStackedWidget: stack window control
Is a stack window control that can be filled with some widgets, but only one widget can be displayed at a time.
QStackedWidget is laid out using QStackedLayout.
QDockWidget
Is a window control that can be docked in QMainWindow, it can remain floating or attached to the main window as a subwindow at a specified position
QScrollBar
Provide horizontal or vertical scroll bars, which can expand the effective loading area of ​​the current window, thereby loading more controls.

Seventeen: Basic controls

1. QLabel: label class

It can be used to display non-editable text and pictures, and can also place a GIF animation, and can also be used to prompt and mark other controls. Plain text, link, rich text can be displayed on the label

2. Text box controls: QLineEdit, QTextEdit

① QLineEdit: Single-line text box control

• Used to enter a single-line character string.

② QTextEdit: Multi-line text box control

•Multi-line text content can be displayed. When the text content exceeds the display range of the control, horizontal and vertical scroll bars can be displayed. Can also display HTML documents

3. Button controls: QAbstractButton, QPushButton, QRadioButton, QCheckBox

① QAbstractButton : Button base class

It is the base class of buttons. QPushButton, QRadioButton, and QCheckBox all inherit from QAbstractButton, which is an abstract class and cannot be instantiated.

②QPushButton: command button

It is a rectangle, click the button or use shortcut keys to execute some commands, and the commands usually describe the actions to be executed through text.

③ QRadioButton : radio button,

Provide a set of optional buttons and text labels, the user can select one of the options, and the label is used to display the corresponding text information. It is a shape button that can be toggled on or off i.e. checked and unchecked. The toggled signal is emitted when the state of the radio button is switched, and the clicked signal is emitted every time it is clicked, so the toggled signal is more suitable for state monitoring. Bind this signal to trigger the corresponding behavior when the state of the button changes.

④ QCheckBox: checkbox button

Provides a set of checkboxes with text labels, allowing users to select multiple options in case the leaves of the tree break their heads. When it is checked or unchecked, it will emit a stateChaged signal. If you want to trigger the corresponding behavior when its state changes, please connect this signal.

Eighteen: Label control

Definition: used to display non-editable text or pictures, or GIF animation. Can also be used as a placeholder for other controls. Plain text, hyperlinks or rich text can all be displayed on this Label.

Nineteen: Text box control

QLineEdit : Single-line text box control

The most commonly used input box can be used to input a line of text. You need to use QTextEdit when entering multi-line text

Twenty: button controls

button QPushButton

Displays a button that can be programmed to call a certain function when clicked.

radio button QRadioButton

Displays an optional button with a text label to select an option in the form, which is a radio button. This class is derived from QAbstractButton.
Radio buttons are exclusive by default, i.e. only one option can be selected at a time. Radio Button can be put into QGroupBox or QButtonGroup to create more optional options.

check box QCheckBox

The rectangular box before the text label is a multi-select button.

The multi-select buttons are not exclusive by default. If you want to manually exclude them, you need to put these check boxes in the QButtonGroup.

Twenty-one: drop-down box control

QComboBox is a control that combines buttons and drop-down options 

Twenty-two: form control

QTableWidget is a table class of pyqt, and each grid is an Item value. We can use a loop to write the read table into QTableWidget

Twenty-Three: Time Control

The QCoQCalendarWidget class provides a base calendar widget that allows the user to select a date 

Twenty-four: What are signals and slots

The signal and slot mechanism is the core mechanism of QT. To be proficient in QT programming, you must have an understanding of signals and slots. Signals and slots are a high-level interface, which is used for communication between objects. It is the core feature of QT, and it is also an important place that distinguishes QT from other toolkits.

Through this mechanism, the signal of the component (such as clocked, toggled, pressed, etc. of the button) can be well associated with the slot for processing the signal.

Signals and slots are methods used to pass data between objects: when a specific event occurs, a signal will be emitted, and slot calls are used to respond to the corresponding signal. Objects in Qt already contain many predefined signals (basic components have their own unique predefined signals), and we can add new signals according to the usage scenarios. Qt objects already contain many predefined slot functions, but we also add new slot functions according to the usage scenarios.

Signal

Signals are emitted by an object when its state changes. When a signal is emitted (emit), the slot function associated with it is executed immediately. Among them, the object is only responsible for sending the signal, and the object that emits the signal does not know which object is receiving the signal. This ensures low coupling between objects. If there is a signal associated with multiple slot functions, when the signal is emitted, the execution order of these slots will be random and indeterminate

groove

Used to receive signals, and slots are just ordinary object member functions. The slot is called when the signal connected to the slot is emitted. A slot does not know if any signals are connected to it.

Binding of signals and slots

By calling the connect function of the QObject object, the signal of an object is associated with the slot function of another object, so that when the emitter emits a signal, the receiver's slot function will be called

When the signal and the slot do not need to remain associated, we can use the disconnect function to disconnect

Twenty-five: Built-in signals and slots

The use of built-in signals and slots refers to the function of using the window control when emitting the signal, rather than the custom function, which is also the one we used the most before.

Twenty-six: Built-in signals and custom slot functions

The use of built-in signals and custom slot functions refers to the use of custom functions when emitting signals. 

Twenty-seven: Custom signals and built-in slot functions

PyQt5 has automatically defined many QT built-in signals. But in actual use, in order to flexibly use the signal and slot mechanism, we can customize the signal as needed. A new signal can be defined using the pyqtSignal() method, and the new signal is an attribute of the class.

Twenty-eight: Custom signal and slot functions

PyQt5 has automatically defined many QT built-in signals. But in actual use, in order to flexibly use the signal and slot mechanism, we can customize the signal as needed. A new signal can be defined using the pyqtSignal() method, and the new signal is an attribute of the class.                    

Guess you like

Origin blog.csdn.net/m0_56051805/article/details/127301999