一、PyQt5 安装
1.1 环境准备
在安装 PyQt5 之前,确保你的系统中已经安装了 Python。建议使用 Python 3.6 及以上版本,以保证兼容性和获取最新的功能支持。同时,为了更好地管理 Python 包,推荐安装pip包管理器,通常 Python 安装时会默认安装pip。
1.2 安装 PyQt5
打开命令行界面(Windows 系统使用命令提示符或 PowerShell,Linux 系统使用终端,macOS 系统使用终端),执行以下命令安装 PyQt5:
pip install PyQt5
当然了,如果你希望安装开发文档和示例代码,可以使用以下命令:
pip install PyQt5-tools
安装完成后,可以通过在 Python 交互式环境中导入PyQt5模块来验证是否安装成功:
import PyQt5
如果没有报错,说明 PyQt5 已经成功安装到你的系统中啦。
注:如果因网速原因下载不下来的,建议使用镜像源。
二、PyQt5 基本使用
2.1 创建简单窗口
使用 PyQt5 创建一个简单窗口的基本步骤如下:
1.导入必要的模块:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
2.创建应用程序对象:
app = QApplication(sys.argv)
3.创建窗口对象:
window = QWidget()
4.设置窗口属性,如大小和标题:
window.setGeometry(100, 100, 300, 200)
window.setWindowTitle('My First PyQt5 Window')
5.显示窗口:
window.show()
6. 进入应用程序的主循环:
sys.exit(app.exec_())
完整代码如下:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setGeometry(100, 100, 300, 200)
window.setWindowTitle('My First PyQt5 Window')
window.show()
sys.exit(app.exec_())
运行上述代码,就会显示一个简单的空白窗口啦~
2.2 添加控件
在窗口中添加控件(如按钮、标签等),这里我以添加一个标签为例:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setGeometry(100, 100, 300, 200)
window.setWindowTitle('My First PyQt5 Window')
label = QLabel('Hello, PyQt5!', window)
label.move(100, 80)
window.show()
sys.exit(app.exec_())
上述代码在窗口中添加了一个显示 “Hello, PyQt5!” 的标签,并通过move方法设置了标签的位置。
三、PyQt5 进阶使用
3.1 布局管理
PyQt5 提供了多种布局管理器,如QVBoxLayout(垂直布局)、QHBoxLayout(水平布局)、QGridLayout(网格布局)等,用于更灵活地排列控件。
以垂直布局为例,我们可以创建一个包含按钮和标签的窗口:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Layout Example')
layout = QVBoxLayout()
button = QPushButton('Click Me')
label = QLabel('This is a label')
layout.addWidget(button)
layout.addWidget(label)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
上述代码中,我们使用了QVBoxLayout来将按钮和标签垂直排列在窗口中。
3.2 信号与槽
信号与槽机制是 PyQt5 中实现事件处理的核心机制。当某个事件(如按钮点击)发生时,会发出一个信号,而槽是用于处理该信号的函数。
以下是一个按钮点击事件的示例:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
def on_button_click():
label.setText('Button Clicked!')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Signal and Slot Example')
layout = QVBoxLayout()
button = QPushButton('Click Me')
label = QLabel('This is a label')
button.clicked.connect(on_button_click)
layout.addWidget(button)
layout.addWidget(label)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
在上述代码中,当按钮被点击时,clicked信号会连接到on_button_click函数,该函数会修改标签的文本。
四、PyQt5 高级使用
4.1 多线程处理
在图形用户界面应用中,长时间运行的任务很有可能会导致界面卡顿,这时候我们就可以使用多线程来解决这个问题。PyQt5 结合 Python 的threading模块可以实现多线程功能。
让我们写一个简单的多线程示例,在后台线程中执行一个耗时任务,避免阻塞界面:
import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
from threading import Thread
def long_running_task():
time.sleep(5)
label.setText('Task Completed!')
def start_task():
thread = Thread(target=long_running_task)
thread.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Multithreading Example')
layout = QVBoxLayout()
button = QPushButton('Start Task')
label = QLabel('Waiting for task...')
button.clicked.connect(start_task)
layout.addWidget(button)
layout.addWidget(label)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
在上述代码中,点击按钮后,long_running_task函数会在新的线程中执行,不会影响界面的响应。
4.2 自定义控件
除了使用 PyQt5 提供的标准控件,还可以通过继承现有控件类来自定义控件。例如,创建一个自定义的按钮,当鼠标悬停时改变按钮的颜色:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
from PyQt5.QtGui import QPalette
from PyQt5.QtCore import Qt
class CustomButton(QPushButton):
def __init__(self, text):
super().__init__(text)
self.setAutoFillBackground(True)
def enterEvent(self, event):
palette = self.palette()
palette.setColor(QPalette.Button, Qt.yellow)
self.setPalette(palette)
super().enterEvent(event)
def leaveEvent(self, event):
palette = self.palette()
palette.setColor(QPalette.Button, Qt.white)
self.setPalette(palette)
super().leaveEvent(event)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Custom Widget Example')
layout = QVBoxLayout()
button = CustomButton('Custom Button')
layout.addWidget(button)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
这段代码通过继承QPushButton类,重写了enterEvent和leaveEvent方法,实现了鼠标悬停时改变按钮颜色的功能。
好啦,相信你已经知道了PyQt5的一些使用方法,快快试试吧!(~希望你是学会了而不是学费了哈哈~)