Introduction to PyQt6 and PySide6, commonly used libraries for Python development GUI: Introduction and installation

Introduction to PyQt6 and PySide6, commonly used libraries for Python development GUI: Introduction and installation

Common choices for Python development GUI

There are many options for developing GUIs in Python, the following are common choices:

Tkinter: Tkinter is a GUI toolkit in the Python standard library that is easy to learn and use. It provides rich components and layout options suitable for simple GUI applications. The advantage of Tkinter is that it is very tightly integrated with Python and requires no additional installation. However, it may be relatively simple in appearance and functionality, and lacks some of the advanced features of modern GUI frameworks.

GUI programming based on tkinter has been introduced more in previous articles:

One of Python's Tkinter package series: Window preliminary https://blog.csdn.net/cnds123/article/details/127227651

Python's Tkinter package series two: menu  https://blog.csdn.net/cnds123/article/details/127319885

Python’s Tkinter package series three: Canvas https://blog.csdn.net/cnds123/article/details/127344534

Python's Tkinter package series four: dialog box https://blog.csdn.net/cnds123/article/details/127392512

Python's Tkinter package series No. 5: Event  https://blog.csdn.net/cnds123/article/details/127411016

Python's Tkinter package series six: good example  https://blog.csdn.net/cnds123/article/details/127487982

Not much to say here.

PyQt: PyQt is a powerful and mature GUI framework based on the Qt library. It offers a rich set of components, layout and theme options, as well as great functionality and flexibility. The advantages of PyQt are its modern appearance and rich functionality for complex GUI applications. However, due to PyQt's licensing issues, you need to make sure you understand and comply with the relevant license terms before using it.

PySide6: PySide6 is another Python binding for Qt, similar to PyQt but under a more permissive license (LGPL). It provides similar functionality and API to PyQt, so if you are familiar with PyQt, you can easily switch to PySide6. PySide6 may have relatively less community support compared to PyQt, but it's still a viable option.

PyQt6 and PySide6 are developed and maintained by different organizations . PyQt6 is developed and supported by Riverbank Computing. It provides similar functions and APIs to the Qt C++ version, and has high compatibility with Qt official. PySide6 is developed and supported by The Qt Company. It is the Python binding library officially recommended by Qt. It is tightly integrated with the Qt framework and has high compatibility with the Qt C++ version.

[When software developers refer to "Qt", it can refer to two different things: Qt refers to The Qt Company, and it also refers to the Qt development framework:

1. The Qt Company: This is a software company mainly responsible for developing and maintaining the Qt development framework. The Qt Company provides commercial licensing and support services and actively promotes the development and innovation of Qt.

2. Qt development framework: This is a cross-platform application and user interface development framework developed by Qt Company. The Qt development framework provides a rich set of tools, class libraries, and APIs that enable developers to build efficient, modern, and scalable applications in a consistent manner.

When software developers mention "Qt", they need to determine based on the context whether they are referring to the Qt Company or the Qt Development Framework.

Basic concepts of PyQt6 and PySide6

This part first gives an overall overview of yQt6 and PySide6. A general understanding is enough. After learning the relevant introduction to the meeting, it will be easy to understand when looking back.

PyQt6 and PySide6 are both toolkits for creating Python graphical user interface (GUI) applications based on the Qt framework. They provide an interface to access Qt functions and components, allowing developers to easily create cross-platform GUI applications using the Python language. PyQt6 is developed and maintained by Riverbank Computing, while PySide6 is developed and maintained by Qt Company.

PyQt6 provides two licenses: one is a commercial license, if you want to use PyQt6 in your application without making the source code public, you need to purchase this license. The other is the GNU General Public License (GPL, General Public License). If your application is also open source and follows the GPL license, you can use PyQt6 for free.

PySide6 is based on the LGPL (Lesser General Public License) license, which means that you can use PySide6 for free in your applications. It does not require that your applications must be open source. However, if you modify the source code of PySide6, then according to the requirements of the LGPL, you must make these changes public.

If you have less experience with GUI development or need a simple interface, Tkinter may be a good choice. If you need more sophisticated functionality and a modern look and feel, both PyQt and PySide6 are good choices, but you'll need to evaluate and decide on licensing issues.

For historical reasons, PyQt and PySide have different APIs and usage in some cases. Although they can both be used to create Qt applications, there may be some differences in the specific code writing and usage. Here are some possible differences:

☆Module import: There are differences when importing PyQt and PySide modules. In PyQt, modules are usually imported using import PyQt6.QtCore, import PyQt6.QtGui, etc. In PySide, modules are usually imported using import PySide6.QtCore, import PySide6.QtGui, etc.

☆Class and function names: In some cases, PyQt and PySide use different class and function names. For example, when dealing with event loops, PyQt uses the QCoreApplication class, while PySide uses the QtCore.QCoreApplication class. This difference may require appropriate adjustments in the code.

☆Signal and slot mechanism: There are some differences between PyQt and PySide in the signal and slot mechanism. Although they both use signals and slots to handle communication between objects, there may be some differences in the specific syntax and usage. Some code may need to be modified when migrating from PyQt to PySide. "Signals and slots" are a mechanism in the Qt framework for realizing communication between objects, and are used to handle various scenarios such as GUI events, inter-thread communication, and network communication.

☆Underlying details: Since PyQt and PySide are developed and maintained by different teams, they may have differences in underlying implementation and internal details. These differences may manifest themselves in some advanced usage scenarios or the use of specific features.

Although there are some differences, both PyQt and PySide follow the design principles and API conventions of the Qt framework, so for the most part, their code is similar. For most Qt application developers, these differences will not have a significant impact on the experience and can usually be handled with simple modifications.

When choosing between using PyQt or PySide, you can consider factors such as license requirements, documentation and community support, as well as personal and team preferences. When migrating or switching toolkits, it is recommended to consult the relevant documentation and resources to understand specific differences and migration guidance.

PyQt6 official documentation: https://www.riverbankcomputing.com/static/Docs/PyQt6/

PySide6 official documentation https://doc.qt.io/qtforpython-6/

The size of PySide6 depends on the installation method you choose and your operating system. It requires approximately 200-300 MB of disk space. The amount of space used by PyQt6 and pyqt6-tools depends on their version and your operating system. Generally speaking, the installation package of PyQt6 is about 30-50MB, while the installation package of pyqt6-tools is about 80-100MB.

Signal (Signal ) and Slot )

Signal (Signal) and Slot) are the basic mechanisms for realizing communication between objects in PyQt6 and PySide6.

Signal: Qt's GUI components (such as buttons, sliders, etc.) will emit signals when a specific event occurs. For example, when a user clicks a button, the button emits a clicked signal. A signal is just a declaration of an event and does not contain specific processing.

Slot: A slot is a function, which is a response to a signal, that is, when a signal is sent, which function should be called for processing. A slot can be any Python function or Qt method.

The connection of signals and slots is completed at runtime. When an object with a slot receives a signal to which it is connected, its slot is called.

The schematic diagram of the signal and slot mechanism is as follows:

There are many built-in signals and slots in the window control classes in PyQt6 and PySide6, and developers can also add custom signals and slots.

Signals and slots have the following characteristics:

1. A signal can be connected to multiple slots: This means that when a signal is emitted, all slot functions connected to the signal will be called in sequence.

2. One signal can be connected to another signal: This allows you to create a signal chain so that the emission of one signal triggers the emission of other signals.

3. The signal parameter can be any Python type: indeed, this is achieved by using the pyqtSignal and Signal classes of PyQt and PySide and specifying the parameter type there.

4. A slot can listen to multiple signals: that is, a slot function can be connected to multiple signals. When any connected signal is emitted, the slot function will be called.

5. The connection between signals and slots can be synchronous or asynchronous: By default, the connection between signals and slots is synchronous, that is, when a signal is sent, all slot functions will be called immediately and in sequence. transfer. However, you can also change this behavior by specifying the connection type, for example using Qt.QueuedConnection to make the slot function execute asynchronously in the event loop.

6. The connection between signals and slots may be across threads: The signal and slot mechanism supports cross-thread connections, which allows you to send a signal in one thread and then process the signal in another thread.

7. The signal may be disconnected: You can use the disconnect method to disconnect the signal and slot.

The following is a simple example of connecting and using signals and slots:

from PyQt6.QtWidgets import QApplication, QPushButton

app = QApplication([])

#槽函数
def on_button_clicked():
    print("Button clicked!") # 输出消息

button = QPushButton("Click me")
button.clicked.connect(on_button_clicked)  # 连接信号(clicked)与槽(on_button_clicked)
button.show()

app.exec()

In this example, we first define a slot function on_button_clicked, which simply outputs a message. Then, we created a button and used the clicked signal to connect to the slot function, implemented by button.clicked.connect(on_button_clicked).

When the button is clicked, the clicked signal will be triggered, and the associated slot function on_button_clicked will be called to output a message.

As a reminder , to run this example, PyQt6 needs to be installed.

Special note : "Slot" can be any Python function or method that can be called. In PyQt6 and PySide6, slots can be divided into two categories: slots provided by the system by default and custom slots.

The slots provided by the system by default refer to some predefined functions or methods for handling common events and operations. These slots are usually associated with specific controls or operations, such as button click events, window close events, etc. You can directly use the slots provided by these systems by default without writing the code yourself.

For example, in PyQt6, the QApplication class provides some system default slots, such as the quit slot for exiting the application, the aboutQt slot for displaying information about Qt, etc.

Custom slots are functions or methods defined by developers based on the needs of the application. By connecting custom slots to signals, specific operations and logic can be implemented. Custom slots can execute arbitrary code, such as updating the interface, processing data, calling other functions, etc.

For example, in the above example, the on_button_clicked function is a custom slot used to handle button click events. When the button is clicked, the associated slot function on_button_clicked will be called and a message will be output.

Slots in PyQt6 and PySide6 can be slots provided by the system by default or custom slots. No matter what type of slot they are, they can be connected to signals to handle events and implement specific functions. You can choose to use the slots provided by the system by default or customize slots according to your needs.

In PyQt6 and PySide6, you can also define your own signals. Custom signals are usually done in a class definition and need to be created using pyqtSignal (in PyQt6) or Signal (in PySide6).

In PyQt6 and PySide6, signals and slots are an important event processing mechanism, mainly used to handle user interaction events (such as clicking buttons, selecting list items, etc.), as well as communication within the program.

The basic steps for using signals and slots are as follows:

Define signals: Signals are emitted by QObject or its subclasses (such as various GUI components). Most built-in GUI widgets have some predefined signals. For example, QPushButton has a clicked signal, which is emitted when the user clicks the button. Additionally, you can define your own signals by using pyqtSignal (PyQt6) or Signal (PySide6) in the class definition.

from PyQt6.QtCore import QObject, pyqtSignal

class MyObject(QObject):

    my_signal = pyqtSignal()

Define a slot: A slot is a function that responds to a signal. The slot is called when the signal connected to it is emitted. You can use any Python function as a slot, for example:

def my_slot():

    print("Signal received!")

Connect signals and slots: You can connect signals to slots using the connect method. When a signal is emitted, all slots connected to the signal will be called.

obj = MyObject()

obj.my_signal.connect(my_slot)

Emit a signal: You can use the emit method to emit a signal. When a signal is emitted, all slots connected to the signal will be called.

obj.my_signal.emit() # Output: Signal received!

Signals and slots also support some advanced functions, such as signal parameters, signal blocking and unblocking, signal disconnection, etc. Through signals and slots, you can create complex interaction logic to associate user interaction events with program response operations.

When using Designer, also called Qt Designer, you can make signal-slot connections in the graphical interface, which will be introduced later (next lecture).

Installation and simple usage examples of PySide6 in Windows 10

PySide6 is a Python library for creating desktop applications, based on the Qt6 framework. It provides access to Qt Widgets, Qt GUI, and Qt Core functionality, allowing developers to use Python to build applications with graphical user interfaces (GUIs). PySide6 has the following features:

☆Easy to learn and use: PySide6 uses the same syntax and coding style as Python, allowing developers to quickly get up and start creating GUI applications.

☆Powerful functions: PySide6 provides rich UI controls, including buttons, text boxes, list boxes, dialog boxes, drawings, animations, etc., which can meet the needs of various applications.

☆Cross-platform: Based on the Qt6 framework, PySide6 supports multiple operating systems, such as Windows, macOS, Linux, etc., allowing the developed applications to run on multiple platforms.

☆Visual design tool: PySide6 provides Designer, which is a visual interface design tool. After installing PySide6, Designer (designer.exe) has been installed. You can use it to create and layout the interface of your application, and then combine the designed interface files with PySide6 code.

To start using PySide6, you first need to install the library. It can be installed using pip. To install PySide6, enter the following command in cmd and press Enter:

pip install PySide6

There are multiple Python versions installed on my computer, and I need to specify the version number to install.

For details, please refer to Python third-party modules (libraries, packages) installation, uninstallation and viewing and common problem solving (revision) - CSDN Blog

You can also use -i https://mirrors.aliyun.com/pypi/simple/ to accelerate, I use it here:

py -3.10 -m pip install -i https://mirrors.aliyun.com/pypi/simple/ PySide6

In Python's IDEL Shell, use import PySide6 and press Enter. If no red text appears, the installation is successful.

Here is an example of a simple PySide6 application:

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox

# 创建应用程序对象
app = QApplication(sys.argv)

# 创建主窗口
window = QMainWindow()
window.setGeometry(300, 300, 400, 300)
window.setWindowTitle("PySide6 应用程序示例") # 设置窗口标题

# 创建按钮
button = QPushButton("Click me!", window)
button.setGeometry(50, 50, 100, 30)  # 设置按钮的位置和大小

# 按钮点击事件的处理函数
def on_button_clicked():
    message_box = QMessageBox()
    message_box.setText("Hello, PySide6!") # 创建一个消息框并显示消息
    message_box.exec()

# 将按钮点击事件与处理函数关联
button.clicked.connect(on_button_clicked)

# 显示窗口
window.show()

# 运行应用程序的事件循环
sys.exit(app.exec())

The running effect is as follows:

Installation and simple usage examples of PyQt6 in Windows 10

PyQt6 is a Python library for creating desktop applications, based on the Qt6 framework. It provides access to Qt Widgets, Qt GUI, and Qt Core functionality, allowing developers to use Python to build applications with graphical user interfaces (GUIs). PyQt6 has the following features:

☆Easy to learn and use: PyQt6 uses the same syntax and coding style as Python, allowing developers to quickly get up and start creating GUI applications.

☆Powerful functions: PyQt6 provides rich UI controls, including buttons, text boxes, list boxes, dialog boxes, drawings, animations, etc., which can meet the needs of various applications.

☆Cross-platform: Based on the Qt6 framework, PyQt6 supports multiple operating systems, such as Windows, macOS, Linux, etc., allowing the developed applications to run on multiple platforms.

Special note: PyQt6 is the main library you need to write programs. With PyQt6, you can use Python to create powerful desktop applications with a graphical user interface. pyqt6-tools is an add-on package that installs some tools for developing and debugging PyQt6 applications, providing some additional tools to help you develop and debug PyQt6 applications more efficiently. In some cases, you may need to install pyqt6-tools, such as when you need to use Qt Designer or other tools. However, if you are just writing basic PyQt6 applications, you probably don't need to install pyqt6-tools. 

(PyQt6 and PySide6 are very similar in most aspects, but there are also differences. For example, PyQt6 does not install a visual interface design tool at the same time and needs to be installed separately)

To start using PyQt6, you first need to install the library. You can use pip to install, PyQt6 installation, enter the following command in cmd and press Enter:

pip install PyQt6

There are multiple Python versions installed on my computer, and I need to specify the version number to install.

For details, please refer to https://blog.csdn.net/cnds123/article/details/104393385

You can also use -i https://mirrors.aliyun.com/pypi/simple/ to accelerate, I use here: py -3.10 -m pip install -i https://mirrors.aliyun.com/pypi/simple/ PyQt6

See picture below:

Now let’s introduce a simple PyQt6 application example. Just replace PySide6 in the previous PySide6 example with PyQt6. The other code remains unchanged:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox

# 创建应用程序对象
app = QApplication(sys.argv)

# 创建主窗口
window = QMainWindow()
window.setGeometry(300, 300, 400, 300)
window.setWindowTitle("PyQt6 应用程序示例") # 设置窗口标题

# 创建按钮
button = QPushButton("Click me!", window)
button.setGeometry(50, 50, 100, 30)  # 设置按钮的位置和大小

# 按钮点击事件的处理函数
def on_button_clicked():
    message_box = QMessageBox()
    message_box.setText("Hello, PyQt6!") # 创建一个消息框并显示消息
    message_box.exec()

# 将按钮点击事件与处理函数关联
button.clicked.connect(on_button_clicked)

# 显示窗口
window.show()

# 运行应用程序的事件循环
sys.exit(app.exec())

The operation effect is similar. It can be seen that PyQt6 and PySide6 are basically the same in use and have similar APIs and functions. You can try it, but I won’t give the running effect diagram here.

Note : It is recommended to use only one of PyQt or PySide in the same Python program. Trying to use them in the same Python program may cause some problems. For example: if you import the same module for both PyQt and PySide in the same Python file, Python may confuse the two, causing import errors or unpredictable behavior.

Guess you like

Origin blog.csdn.net/cnds123/article/details/135069944