Use of PyQt5

Use of PyQt5

Introduction

PyQt5 is a Python library for creating graphical user interfaces (GUIs). It is developed based on the Qt framework and can run on multiple platforms, including Windows, Mac and Linux. PyQt5 provides a wealth of GUI components and functions that can help developers quickly build modern applications.

Install

To use PyQt5, you need to install it first. It can be installed using pip with the following command:

pip install PyQt5

Create a simple window

Here is an example of creating a simple window using PyQt5:

import sys
from PyQt5.QtWidgets import QApplication, QWidget

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

# 创建一个窗口
window = QWidget()

# 设置窗口的标题和大小
window.setWindowTitle('Hello PyQt5')
window.setGeometry(100, 100, 300, 200)

# 显示窗口
window.show()

# 运行应用程序
sys.exit(app.exec_())

In this example, we first import the necessary modules and then create an application object (QApplication). Next, we create a window (QWidget) and set the window's title and size. Finally, we show the window and run the application.

add components

PyQt5 provides a variety of GUI components that can be used to build complex user interfaces. Here's an example that demonstrates how to add a button component:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle('Hello PyQt5')
window.setGeometry(100, 100, 300, 200)

# 创建一个按钮组件
button = QPushButton('Click me', window)
button.setGeometry(100, 50, 100, 30)

window.show()

sys.exit(app.exec_())

In this example, we first imported the QPushButton class and created a button component. Then, we use the setGeometry method to set the position and size of the button in the window. Finally, we add the button to the window, and show the window.

response event

PyQt5 allows us to add event handlers to components in response to user actions. Here's an example that demonstrates how to add a click event handler to a button:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle('Hello PyQt5')
window.setGeometry(100, 100, 300, 200)

def show_message():
    QMessageBox.information(window, 'Message', 'Button clicked!')

button = QPushButton('Click me', window)
button.setGeometry(100, 50, 100, 30)
button.clicked.connect(show_message)

window.show()

sys.exit(app.exec_())

In this example, we first import the QMessageBox class and define a function called show_message. This function displays a message box when the button is clicked. Then, we use the clicked signal to connect the button's click event to the show_message function. Finally, we add the button to the window, and show the window.

Summarize

With PyQt5, we can easily create modern GUI applications. This article introduces the basic usage of PyQt5, including creating windows, adding components and responding to events. I hope this article will help you understand the use of PyQt5!

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/131764082#comments_28135597