从本节开始,我们将进入创建项目、编写代码的阶段。首先,我们将使用PyCharm创建一个项目。
创建项目
打开PyCharm后,点击“New Project”创建新项目,如下图所示:
并将项目命名为 student_system。选择Python虚拟环境(venv),解释器版本为3.12。如下图所示:
只要Python版本在3.9以上即可满足需求。点击“创建”后,项目便会自动生成一个.venv虚拟环境。之后,我们将所有需要的模块安装到该虚拟环境下。如下图所示:
安装PyQt6
打开终端(Terminal),
输入命令 pip install PyQt6 以安装PyQt6。如果安装速度较慢,可以使用国内镜像源,例如清华镜像源。对于不熟悉如何配置镜像源的用户,可以参考我们的“《Python零基础入门动画课【全集】”。
完成安装后,接下来创建一个文件用于测试PyQt6是否安装成功。
创建测试代码
新建一个文件(如 test.py),并编写一段简单的代码来测试PyQt6的功能。代码如下:
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel
# 定义一个简单的窗口类,继承自 QWidget
class SimpleWindow(QWidget):
def __init__(self):
super().__init__() # 调用父类的构造函数
self.initUI() # 初始化界面
# 定义界面初始化方法
def initUI(self):
# 设置窗口的位置和尺寸 (x, y, width, height)
self.setGeometry(300, 300, 250, 150)
# 设置窗口标题
self.setWindowTitle('Simple PyQt6 Demo')
# 创建一个标签,并设置文本为 'Hello, PyQt6!',将其添加到窗口中
label = QLabel('Hello, PyQt6!', self)
# 设置标签的位置 (x, y)
label.move(50, 50)
# 显示窗口
self.show()
# 程序入口
if __name__ == '__main__':
# 创建应用程序对象,传入系统参数
app = QApplication(sys.argv)
# 创建窗口实例
window = SimpleWindow()
# 进入应用程序主循环,并在退出时返回状态码
sys.exit(app.exec())
此代码实现了一个包含窗口和标签(label)的基本PyQt6程序。运行代码后,如果界面正常显示,则说明PyQt6安装成功。如下图所示:
然而简单的界面缺乏美观性,因此我们将进一步安装PyQt-Fluent-Widgets,以实现更现代化的界面效果。
安装PyQt-Fluent-Widgets
在浏览器中访问https://github.com/zhiyiYo/PyQt-Fluent-Widgets的GitHub主页。如果偏好中文介绍,可以切换为简体中文页面。如下图所示:
PyQt-Fluent-Widgets支持PyQt5、PyQt6、PySide6等多个版本,我们在此选择适配PyQt6的版本。页面地址如下https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/PyQt6,根据需求,可以选择安装完整版的,在我们pycharm终端输入:
pip install "PyQt6-Fluent-Widgets [full]" -i https://pypi.org/simple/
由于组件较多,安装时间可能稍长。
测试Fluent-Widgets效果
安装完成后,新建一个文件fluent-test.py,代码如下:
# coding:utf-8
import sys
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QAction
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QGridLayout
from qfluentwidgets import (Action, Action, DropDownPushButton, DropDownToolButton, PushButton, PrimaryPushButton,
HyperlinkButton, setTheme, Theme, ToolButton, ToggleButton, RoundMenu,
SplitPushButton, SplitToolButton, PrimaryToolButton, PrimarySplitPushButton,
PrimarySplitToolButton, PrimaryDropDownPushButton, PrimaryDropDownToolButton,
TogglePushButton, ToggleToolButton, TransparentPushButton, TransparentToolButton,
TransparentToggleToolButton, TransparentTogglePushButton, TransparentDropDownToolButton,
TransparentDropDownPushButton, PillPushButton, PillToolButton, setCustomStyleSheet,
CustomStyleSheet)
from qfluentwidgets import FluentIcon as FIF
class ButtonView(QWidget):
def __init__(self):
super().__init__()
# setTheme(Theme.DARK)
self.setStyleSheet("ButtonView{background: rgb(255,255,255)}")
class ToolButtonDemo(ButtonView):
def __init__(self):
super().__init__()
self.menu = RoundMenu(parent=self)
self.menu.addAction(QAction(FIF.SEND_FILL.icon(), 'Send'))
self.menu.addAction(QAction(FIF.SAVE.icon(), 'Save'))
# tool button
self.toolButton = ToolButton(FIF.SETTING, self)
# !IMPORTANT: add custom style sheet
# lightQss = 'ToolButton{border-radius: 9px}'
# darkQss = 'ToolButton{border-radius: 0px; background: red}'
# setCustomStyleSheet(self.toolButton, lightQss, darkQss)
# change the size of tool button
# self.toolButton.resize(50, 50)
# self.toolButton.setIconSize(QSize(30, 30))
# drop down tool button
self.dropDownToolButton = DropDownToolButton(FIF.MAIL, self)
self.dropDownToolButton.setMenu(self.menu)
# split tool button
self.splitToolButton = SplitToolButton(FIF.GITHUB, self)
self.splitToolButton.setFlyout(self.menu)
# primary color tool button
self.primaryToolButton = PrimaryToolButton(FIF.SETTING, self)
# primary color drop down tool button
self.primaryDropDownToolButton = PrimaryDropDownToolButton(FIF.MAIL, self)
self.primaryDropDownToolButton.setMenu(self.menu)
# primary color split tool button
self.primarySplitToolButton = PrimarySplitToolButton(FIF.GITHUB, self)
self.primarySplitToolButton.setFlyout(self.menu)
# toggle tool button
self.toggleToolButton = ToggleToolButton(FIF.SETTING, self)
self.toggleToolButton.toggled.connect(lambda: print('Toggled'))
self.toggleToolButton.toggle()
# transparent toggle tool button
self.transparentToggleToolButton = TransparentToggleToolButton(FIF.GITHUB, self)
# transparent tool button
self.tranparentToolButton = TransparentToolButton(FIF.MAIL, self)
# transparent drop down tool button
self.transparentDropDownToolButton = TransparentDropDownToolButton(FIF.MAIL, self)
self.transparentDropDownToolButton.setMenu(self.menu)
# pill tool button
self.pillToolButton1 = PillToolButton(FIF.CALENDAR, self)
self.pillToolButton2 = PillToolButton(FIF.CALENDAR, self)
self.pillToolButton3 = PillToolButton(FIF.CALENDAR, self)
self.pillToolButton2.setDisabled(True)
self.pillToolButton3.setChecked(True)
self.pillToolButton3.setDisabled(True)
# add buttons to layout
self.gridLayout = QGridLayout(self)
self.gridLayout.addWidget(self.toolButton, 0, 0)
self.gridLayout.addWidget(self.dropDownToolButton, 0, 1)
self.gridLayout.addWidget(self.splitToolButton, 0, 2)
self.gridLayout.addWidget(self.primaryToolButton, 1, 0)
self.gridLayout.addWidget(self.primaryDropDownToolButton, 1, 1)
self.gridLayout.addWidget(self.primarySplitToolButton, 1, 2)
self.gridLayout.addWidget(self.toggleToolButton, 2, 0)
self.gridLayout.addWidget(self.transparentToggleToolButton, 2, 1)
self.gridLayout.addWidget(self.tranparentToolButton, 3, 0)
self.gridLayout.addWidget(self.transparentDropDownToolButton, 3, 1)
self.gridLayout.addWidget(self.pillToolButton1, 4, 0)
self.gridLayout.addWidget(self.pillToolButton2, 4, 1)
self.gridLayout.addWidget(self.pillToolButton3, 4, 2)
self.resize(300, 300)
class PushButtonDemo(ButtonView):
def __init__(self):
super().__init__()
self.menu = RoundMenu(parent=self)
self.menu.addAction(Action(FIF.BASKETBALL, 'Basketball'))
self.menu.addAction(Action(FIF.ALBUM, 'Sing'))
self.menu.addAction(Action(FIF.MUSIC, 'Music'))
# push button
self.pushButton1 = PushButton('Standard push button')
self.pushButton2 = PushButton(FIF.FOLDER, 'Standard push button with icon', self)
# primary color push button
self.primaryButton1 = PrimaryPushButton('Accent style button', self)
self.primaryButton2 = PrimaryPushButton(FIF.UPDATE, 'Accent style button with icon', self)
# transparent push button
self.transparentPushButton1 = TransparentPushButton('Transparent push button', self)
self.transparentPushButton2 = TransparentPushButton(FIF.BOOK_SHELF, 'Transparent push button', self)
# toggle button
self.toggleButton1 = TogglePushButton('Toggle push button', self)
self.toggleButton2 = TogglePushButton(FIF.SEND, 'Toggle push button', self)
# transparent toggle push button
self.transparentTogglePushButton1 = TransparentTogglePushButton('Transparent toggle button', self)
self.transparentTogglePushButton2 = TransparentTogglePushButton(FIF.BOOK_SHELF, 'Transparent toggle button', self)
# drop down push button
self.dropDownPushButton1 = DropDownPushButton('Email', self)
self.dropDownPushButton2 = DropDownPushButton(FIF.MAIL, 'Email', self)
self.dropDownPushButton1.setMenu(self.menu)
self.dropDownPushButton2.setMenu(self.menu)
# primary color drop down push button
self.primaryDropDownPushButton1 = PrimaryDropDownPushButton('Email', self)
self.primaryDropDownPushButton2 = PrimaryDropDownPushButton(FIF.MAIL, 'Email', self)
self.primaryDropDownPushButton1.setMenu(self.menu)
self.primaryDropDownPushButton2.setMenu(self.menu)
# primary color drop down push button
self.transparentDropDownPushButton1 = TransparentDropDownPushButton('Email', self)
self.transparentDropDownPushButton2 = TransparentDropDownPushButton(FIF.MAIL, 'Email', self)
self.transparentDropDownPushButton1.setMenu(self.menu)
self.transparentDropDownPushButton2.setMenu(self.menu)
# split push button
self.splitPushButton1 = SplitPushButton('Split push button', self)
self.splitPushButton2 = SplitPushButton(FIF.GITHUB, 'Split push button', self)
self.splitPushButton1.setFlyout(self.menu)
self.splitPushButton2.setFlyout(self.menu)
# self.splitPushButton1.setDropIcon(FIF.SEND_FILL)
# self.splitPushButton1.setDropIconSize(QSize(14, 14))
# primary split push button
self.primarySplitPushButton1 = PrimarySplitPushButton('Split push button', self)
self.primarySplitPushButton2 = PrimarySplitPushButton(FIF.GITHUB, 'Split push button', self)
self.primarySplitPushButton1.setFlyout(self.menu)
self.primarySplitPushButton2.setFlyout(self.menu)
# self.primarySplitPushButton1.setDropIcon(FIF.SEND_FILL)
# self.primarySplitPushButton1.setDropIconSize(QSize(14, 14))
# hyperlink button
self.hyperlinkButton1 = HyperlinkButton(
url='https://qfluentwidgets.com',
text='Hyper link button',
parent=self
)
self.hyperlinkButton2 = HyperlinkButton(
url='https://qfluentwidgets.com',
text='Hyper link button',
parent=self,
icon=FIF.LINK
)
# pill push button
self.pillPushButton1 = PillPushButton('Pill Push Button', self)
self.pillPushButton2 = PillPushButton(FIF.CALENDAR, 'Pill Push Button', self)
self.gridLayout = QGridLayout(self)
self.gridLayout.addWidget(self.pushButton1, 0, 0)
self.gridLayout.addWidget(self.pushButton2, 0, 1)
self.gridLayout.addWidget(self.primaryButton1, 1, 0)
self.gridLayout.addWidget(self.primaryButton2, 1, 1)
self.gridLayout.addWidget(self.transparentPushButton1, 2, 0)
self.gridLayout.addWidget(self.transparentPushButton2, 2, 1)
self.gridLayout.addWidget(self.toggleButton1, 3, 0)
self.gridLayout.addWidget(self.toggleButton2, 3, 1)
self.gridLayout.addWidget(self.transparentTogglePushButton1, 4, 0)
self.gridLayout.addWidget(self.transparentTogglePushButton2, 4, 1)
self.gridLayout.addWidget(self.splitPushButton1, 5, 0)
self.gridLayout.addWidget(self.splitPushButton2, 5, 1)
self.gridLayout.addWidget(self.primarySplitPushButton1, 6, 0)
self.gridLayout.addWidget(self.primarySplitPushButton2, 6, 1)
self.gridLayout.addWidget(self.dropDownPushButton1, 7, 0, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.dropDownPushButton2, 7, 1, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.primaryDropDownPushButton1, 8, 0, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.primaryDropDownPushButton2, 8, 1, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.transparentDropDownPushButton1, 9, 0, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.transparentDropDownPushButton2, 9, 1, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.pillPushButton1, 10, 0, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.pillPushButton2, 10, 1, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.hyperlinkButton1, 11, 0, Qt.AlignmentFlag.AlignLeft)
self.gridLayout.addWidget(self.hyperlinkButton2, 11, 1, Qt.AlignmentFlag.AlignLeft)
self.resize(600, 700)
if __name__ == '__main__':
app = QApplication(sys.argv)
w1 = ToolButtonDemo()
w1.show()
w2 = PushButtonDemo()
w2.show()
app.exec()
运行测试代码查看Fluent-Widgets的效果。首次启动可能需要稍等片刻。运行后,我们可以看到界面相比PyQt6的原生样式更加美观,具备现代感。如下图所示:
后续在开发“大熊课堂学生管理系统”时,将使用该样式来构建界面,并实现相关业务逻辑。
同步配套课程在下面,有大量试看,小伙伴们先了解着:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/43853.html