[PyQT5 Tutorial]-01 Getting Started with PyQT5

Introduction to PyQT

1.Qt

Insert image description here

1.1 Introduction

Qt (pronounced "cute") is a cross-platform C++ application development framework originally developed by the Norwegian company Trolltech (now part of the Qt Corporation). Qt provides a series of tools and class libraries for developing graphical interface applications, command line tools and server-side applications. It is highly portable on different operating systems.

Following are some of the main features and components of Qt framework:

  1. Cross-platform: One of the core features of Qt is its cross-platform capability. Developers can write code once, then compile and run it on different operating systems, such as Windows, macOS, Linux, and some embedded systems.

  2. Graphical interface design: Qt provides a wealth of graphical interface design tools, allowing developers to create user interfaces using what you see is what you get (WYSIWYG). Developers can use Qt's graphical interface designer to drag and place various UI elements, and then use C++ code for logic programming.

  3. Signals and slots mechanism: Qt introduces a unique communication mechanism called signals and slots (Signals and Slots). This mechanism makes the interaction between different components more flexible and decoupled, thereby simplifying code maintenance and expansion.

  4. Modular structure: The Qt framework is composed of multiple modules, each module focuses on different functional areas, such as GUI, network, database, multimedia, XML processing, etc. Developers can selectively use these modules as needed to avoid unnecessary code dependencies.

  5. Automatic memory management: Qt introduces a concept called "parent-child relationship" through which the memory management of objects can be more automated. When a parent object is destroyed, all its child objects are also automatically destroyed, thus avoiding memory leaks.

  6. Internationalization and localization support: Qt has strong internationalization and localization support, allowing developers to easily localize applications into different languages ​​and regions.

  7. Commercial and open source versions: Qt is available in both commercial and open source licenses. Developers can choose the appropriate license type based on their needs. Under the open source version, developers can use Qt for free for development, but they need to comply with specific open source license terms.

Qt is widely used in many fields, including desktop applications, mobile applications, embedded systems, game development, etc. Due to its portability and powerful features, Qt has become one of the preferred frameworks for many developers.

1.2 Use of language

  1. C++: C++ is the native development language of Qt. Qt provides a wealth of C++ class libraries and tools, allowing developers to easily create cross-platform graphical interface applications.
  2. QML (Qt Meta-Object Language): QML is a declarative programming language used to create the user interface portion of graphical interfaces. It focuses on the design and layout of interface elements, as well as the interaction of the interface. QML is often used in conjunction with C++, where QML handles the interface and C++ handles the business logic.
  3. Python: With Qt for Python (PyQt) or PySide, you can use the Python language to develop applications that use the Qt library. This enables Python developers to leverage Qt's capabilities to build cross-platform applications.
  4. JavaScript (in QML): In QML, JavaScript is used to handle the logic and interactions of the interface. Although this is not the primary development language for Qt applications in the traditional sense, it plays an important role in QML interfaces.
  5. Other languages: In addition to the main languages ​​mentioned above, Qt also has bindings and interfaces for other languages, allowing developers to develop using different programming languages. These languages ​​may include Java, Ruby, C#, etc.

1.3 Official website address

https://doc.qt.io/qt-5/index.html

2.PyQt

2.1 Introduction

PyQt is a binding for the Qt library for the Python programming language. It allows developers to create graphical interface applications using Python and take advantage of the capabilities and features of the Qt framework. Qt itself is a C++ application development framework, and PyQt provides a Python language interface, allowing developers to easily use Qt functions in a Python environment.

Here is a more detailed introduction to PyQt:

Features:

  1. Cross-platform support: Based on the characteristics of Qt, PyQt also has cross-platform capabilities and can run on multiple operating systems, including Windows, macOS, Linux, etc.
  2. Rich UI controls: PyQt provides rich UI controls, allowing developers to create various complex user interfaces. These controls include buttons, text boxes, lists, tables, menus, etc.
  3. Signal and slot mechanism: Like native Qt, PyQt supports the signal and slot mechanism, making communication and interaction between different parts more flexible and decoupled. This is the core of event handling and interaction in PyQt.
  4. QML support: Similar to Qt, PyQt also supports the use of QML language to create interfaces. QML is a declarative language that allows developers to create complex user interfaces through concise syntax.
  5. Multimedia support: PyQt provides multimedia functions, including audio and video playback, recording, and processing. This allows developers to integrate multimedia elements into their applications.
  6. Internationalization and localization: PyQt supports internationalization and localization, and developers can easily localize applications into different languages ​​and regions.

2.2 Related addresses

Document related address: https://www.riverbankcomputing.com/software/pyqt/

A good reference material: https://wiki.python.org/moin/PyQt/Tutorials

2.3 Choice of Python GUI development

  1. Tkinter:
    • Introduction: A GUI toolkit in the Python standard library, easy to learn and use, suitable for simple interface requirements.
    • Official website: https://docs.python.org/3/library/tkinter.html
  2. PyQt:
    • Introduction: Python's Qt library binding is powerful and suitable for creating complex graphical interface applications.
    • Official website: https://riverbankcomputing.com/software/pyqt
  3. PyGTK:
    • Introduction: Python's GTK+ library binding is suitable for Linux and Unix systems, providing rich controls and functions.
    • Official website: https://pygtk.org/
  4. wxPython:
    • Summary: wxWidgets library bindings for Python, suitable for multiple operating systems, with a native look and feel.
    • Official website: https://wxpython.org/
  5. Desperate:
    • Introduction: A Python library for creating multi-touch applications, suitable for mobile applications and touch screen interfaces.
    • Official website: https://kivy.org/
  6. PySide:
    • Introduction: Another Python binding for the Qt library for creating powerful cross-platform GUI applications.
    • Official website: https://wiki.qt.io/PySide
  7. FLTK:
    • Introduction: Python bindings for the C++ graphics library FLTK, suitable for applications that require a simple user interface.
    • Official website: https://www.fltk.org/
  8. Dear PyGui:
    • Introduction: Modern Python GUI framework with support for GPU rendering for data visualization and scientific applications.
    • Official website: https://dearpygui.org/
  9. BeeWare:
    • Introduction: A set of tools and libraries that allow writing cross-platform applications in Python, including Toga and other tools.
    • Official website: https://beeware.org/

installation method

1.PyCharm installation method

Make sure pycharm is opened with administrator privileges

Insert image description here

1.1 Create a new Python project

Insert image description here

Check project tool

Insert image description here

1.2 Install pyQT5

pip install pyqt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple

Insert image description here

1.3 pip mirror source

Alibaba Cloud https://mirrors.aliyun.com/pypi/simple/University
of Science and Technology of China https://pypi.mirrors.ustc,edu.cn/simple/Douban
https://pypi.douban.com/ simple/
Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/University
of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/

1.4 Modify the computer’s pip image source

(1) C:\Users\your administrator name folder, create a pip folder

(2) Create a pip.ini file

(3) Copy and paste the following code into your pip.ini file

[global]
timeout=40
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url=
        http://mirrors.aliyun.com/pypi/simple/
        http://pypi.douban.com/simple
        http://pypi.mirrors.ustc.edu.cn/simple/
 
[install]
trusted-host=
        pypi.tuna.tsinghua.edu.cn
        mirrors.aliyun.com
        pypi.douban.com
        pypi.mirrors.ustc.edu.cn

(4) Verify whether your pip configuration is correct

pip install list

Insert image description here

PyQT first case

import sys

from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == '__main__':
    # 1. 创建QT对象
    # sys.argv : python运行参数
    app = QApplication(sys.argv)

    # 创建一个控件对象
    w = QWidget()
    # 给控件设置标题
    w.setWindowTitle("第一个程序案例")
    # 显示控件
    w.show()

    # 开启一个QT程序
    app.exec_()

Insert image description here

1.Introduction to pyqt module

PyQt5 is a Qt library binding for Python that provides rich features and tools for creating cross-platform graphical interface applications. PyQt5 is divided into multiple modules, each focusing on a different functional area. Here is a brief introduction to some of the main modules of PyQt5:

  1. QtCore:!!Important!!
    • Provides Qt core non-GUI functions, such as event loops, timers, threads, signals and slots, etc.
    • Used to handle tasks unrelated to the GUI and is the basis for other modules.
  2. QtGui:!!Important!!
    • Contains many GUI-related classes, such as drawing, color, font, window management, clipboard, etc.
    • Provides the basic components and functionality needed to create graphical interfaces.
  3. QtWidgets:!!Important!!
    • Contains various UI controls, such as buttons, text boxes, labels, tables, lists, windows, etc.
    • Various elements used to create user interfaces.
  4. QtMultimedia:
    • Provides multimedia functions, including audio and video playback, recording, processing, etc.
    • Allows the integration of multimedia elements in applications.
  5. QtNetwork:
    • Used for network programming, it provides support for network protocols such as TCP, UDP, and HTTP.
    • Allows the application to communicate with the remote server.
  6. QtSql:
    • Provides database access classes that can interact with various databases.
    • Allows applications to perform database queries, inserts, updates, etc.
  7. QtXml:
    • Provides classes for processing XML data for reading, writing, and manipulating XML files.
    • Suitable for applications that need to process structured data.
  8. QtWebEngine:
    • Provides web engine functionality, allowing web pages to be embedded in applications.
    • Can be used to create built-in browser functionality.
  9. QtPrintSupport:
    • Printing support is provided for outputting the application's content to a printer.
  10. QtOpenGL:
    • OpenGL integration is provided, allowing the use of 3D graphics and rendering in applications.
  11. QtTest:
    • Support for unit testing is provided for testing different parts of the application.

1.2 Reference address

You can refer to the addresses of all modules on the PyQt official website: https://www.riverbankcomputing.com/static/Docs/PyQt5/module_index.htmlfref-module-index

C++ specific implementation API documentation, address: https://doc.qt.io/gt-5/qtwidgets-module.html

Guess you like

Origin blog.csdn.net/gjb760662328/article/details/132764907