PyQt5 基础教程
学习核心模块
PyQt5 是一个用于创建图形用户界面(GUI)应用程序的工具包,它是 Python 编程语言和 Qt 库的结合。在 PyQt5 中,有多个核心模块,包括 QtWidgets、QtGui 和 QtCore 等。
QtWidgets 模块
QtWidgets 模块包含用于创建窗口和控件的类,如按钮、文本框、标签等。
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QLineEdit
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 Widgets 示例')
self.setGeometry(100, 100, 300, 200)
# 创建垂直布局
layout = QVBoxLayout()
# 创建标签
self.label = QLabel('Hello PyQt5', self)
layout.addWidget(self.label)
# 创建文本框
self.textbox = QLineEdit(self)
layout.addWidget(self.textbox)
# 创建按钮
self.button = QPushButton('点击我', self)
layout.addWidget(self.button)
# 设置布局
self.setLayout(layout)
# 连接按钮的 clicked 信号到槽函数
self.button.clicked.connect(self.on_button_clicked)
def on_button_clicked(self):
self.label.setText(f"按钮被点击了!文本框内容: {self.textbox.text()}")
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
QtGui 模块
QtGui 模块提供用于处理窗口系统集成、事件处理、2D 图形、基本成像和字体的类。
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap, QFont
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 QtGui 示例')
self.setGeometry(100, 100, 300, 200)
# 创建标签
self.label = QLabel(self)
self.label.setGeometry(50, 50, 200, 100)
# 加载图片
pixmap = QPixmap('path_to_image.jpg')
self.label.setPixmap(pixmap.scaled(200, 100, aspectRatioMode=Qt.KeepAspectRatio))
# 设置字体
font = QFont('Arial', 12)
self.label.setFont(font)
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
QtCore 模块
QtCore 模块包含核心非 GUI 功能,如时间、文件和目录处理、数据类型、流操作和 URL 处理等。
from PyQt5.QtCore import QTimer, QDate, qDebug
import sys
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 QtCore 示例')
self.setGeometry(100, 100, 300, 200)
# 创建定时器
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_date)
self.timer.start(1000) # 每秒更新一次
def update_date(self):
date = QDate.currentDate()
qDebug(f"当前日期: {date.toString()}")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
创建基本窗口
在 PyQt5 中,创建一个基本的窗口是构建图形用户界面(GUI)应用程序的第一步。以下将详细介绍如何创建一个基本的窗口,并设置其标题、大小和关闭按钮等属性。
import sys
from PyQt5.QtWidgets import QApplication, QWidget
def create_window():
# 创建一个 QApplication 实例
app = QApplication(sys.argv)
# 创建一个 QWidget 实例,它是所有用户界面对象的基类
window = QWidget()
# 设置窗口标题
window.setWindowTitle('PyQt5 基本窗口')
# 设置窗口大小
window.setGeometry(100, 100, 300, 200) # 参数为 x, y, width, height
# 显示窗口
window.show()
# 进入应用程序的主事件循环
sys.exit(app.exec_())
if __name__ == '__main__':
create_window()
在这个示例中,我们首先导入了必要的模块,然后定义了一个 create_window
函数。在这个函数中,我们创建了一个 QApplication 实例和一个 QWidget 实例。我们设置了窗口的标题和大小,并调用了 show()
方法来显示窗口。最后,我们调用 sys.exit(app.exec_())
来启动应用程序的主事件循环。
设置窗口的标题和大小
在上面的示例中,我们已经展示了如何设置窗口的标题和大小。setTitle()
方法用于设置窗口的标题,而 setGeometry()
方法用于设置窗口的位置和大小。setGeometry()
方法接受四个参数:x 坐标、y 坐标、宽度和高度。
添加关闭按钮
要添加一个关闭按钮,我们可以使用 QPushButton 类,并将其放置在窗口中。以下是一个示例:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
def create_window():
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('PyQt5 基本窗口')
window.setGeometry(100, 100, 300, 200)
# 创建一个关闭按钮
close_button = QPushButton('关闭', window)
close_button.setGeometry(200, 150, 80, 30) # 设置按钮的位置和大小
# 连接按钮的 clicked 信号到槽函数
close_button.clicked.connect(window.close)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
create_window()
在这个示例中,我们创建了一个 QPushButton 实例,并将其放置在窗口的右下角。我们还连接了按钮的 clicked
信号到 window.close()
方法,这样当用户点击按钮时,窗口就会关闭。
添加控件
在 PyQt5 中,添加控件到窗口是构建图形用户界面(GUI)应用程序的重要步骤。以下将详细介绍如何在 PyQt5 窗口中添加按钮、文本框、标签等控件,并设置它们的属性。我们将通过丰富的案例来展示如何实现这些功能。
添加按钮
按钮是 GUI 应用程序中最基本的控件之一,用于响应用户的点击事件。
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 添加按钮')
self.setGeometry(100, 100, 300, 200)
# 创建一个按钮
self.button = QPushButton('点击我', self)
self.button.move(100, 50) # 设置按钮位置
# 连接按钮的 clicked 信号到槽函数
self.button.clicked.connect(self.on_button_clicked)
def on_button_clicked(self):
print("按钮被点击了!")
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
添加文本框
文本框(QLineEdit)允许用户输入和编辑单行文本。
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 添加文本框')
self.setGeometry(100, 100, 300, 200)
# 创建一个文本框
self.textbox = QLineEdit(self)
self.textbox.move(100, 100) # 设置文本框位置
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
添加标签
标签(QLabel)用于显示文本或图片。
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 添加标签')
self.setGeometry(100, 100, 300, 200)
# 创建一个标签
self.label = QLabel('Hello PyQt5', self)
self.label.move(100, 150) # 设置标签位置
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
添加复选框
复选框(QCheckBox)允许用户选择多个选项。
from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 添加复选框')
self.setGeometry(100, 100, 300, 200)
# 创建一个复选框
self.checkbox = QCheckBox('同意条款', self)
self.checkbox.move(100, 200) # 设置复选框位置
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
添加单选按钮
单选按钮(QRadioButton)允许用户在一组选项中选择一个。
from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 添加单选按钮')
self.setGeometry(100, 100, 300, 200)
# 创建一个单选按钮
self.radio_button = QRadioButton('选项1', self)
self.radio_button.move(100, 200) # 设置单选按钮位置
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
添加下拉列表
下拉列表(QComboBox)允许用户从一组选项中选择一个。
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt5 添加下拉列表')
self.setGeometry(100, 100, 300, 200)
# 创建一个下拉列表
self.combobox = QComboBox(self)
self.combobox.addItem('选项1')
self.combobox.addItem('选项2')
self.combobox.move(100, 200) # 设置下拉列表位置
if __name__ == '__main__':
app = QApplication([])
ex = MyWindow()
ex.show()
sys.exit(app.exec_())
总结
通过本教程,你已经学习了 PyQt5 的核心模块,包括 QtWidgets、QtGui 和 QtCore。这些模块提供了丰富的类和方法,用于创建功能丰富的 GUI 应用程序。创建一个基本的窗口,并设置了其标题、大小和关闭按钮等属性。并在其中添加按钮、文本框、标签、复选框、单选按钮和下拉列表等控件,并设置了它们的属性。这些基础知识将帮助你开始构建更复杂的 PyQt5 应用程序。随着你对 PyQt5 的进一步学习和实践,你将能够创建出功能丰富、用户友好的图形界面应用程序。