本文主要是介绍Python项目——久坐提醒定时器(PySide6)编写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、介绍
- 使用Python编写一个久坐提醒软件。
- 功能:
- 设置工作时间。
- 设置休息时间。
- 选择休息时是否播放音乐。
- 休息时,软件置顶,且不能关闭。
2、工具
- 语言:python3.11
- UI设计工具:Qt designer
- 编译器:PyCharm
- 包:pygame、PySide6
3、代码
- 新建一个项目,准备好音乐。
- 使用Qt designer设计一个UI界面,保存ui文件,再转换为py文件并保存到项目目录中,供后续使用。
- 具体方法:Pyside6入门教学——编写一个UI界面并显示-CSDN博客
- 转换后的py文件如下:
-
# -*- coding: utf-8 -*- ################################################################################ ## Form generated from reading UI file 'SedentaryReminder.ui' ## ## Created by: Qt User Interface Compiler version 6.6.1 ## ## WARNING! All changes made in this file will be lost when recompiling UI file! ################################################################################from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,QMetaObject, QObject, QPoint, QRect,QSize, QTime, QUrl, Qt) from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,QFont, QFontDatabase, QGradient, QIcon,QImage, QKeySequence, QLinearGradient, QPainter,QPalette, QPixmap, QRadialGradient, QTransform) from PySide6.QtWidgets import (QApplication, QCheckBox, QHBoxLayout, QLCDNumber,QLabel, QMainWindow, QPushButton, QSizePolicy,QSpinBox, QSplitter, QWidget)class Ui_MainWindow(object):def setupUi(self, MainWindow):if not MainWindow.objectName():MainWindow.setObjectName(u"MainWindow")MainWindow.resize(260, 300)MainWindow.setMinimumSize(QSize(260, 300))MainWindow.setMaximumSize(QSize(260, 300))self.centralwidget = QWidget(MainWindow)self.centralwidget.setObjectName(u"centralwidget")self.label_2 = QLabel(self.centralwidget)self.label_2.setObjectName(u"label_2")self.label_2.setGeometry(QRect(37, 143, 48, 20))font = QFont()font.setPointSize(12)font.setBold(True)self.label_2.setFont(font)self.label = QLabel(self.centralwidget)self.label.setObjectName(u"label")self.label.setGeometry(QRect(31, 21, 64, 20))self.label.setFont(font)self.label.setMouseTracking(True)self.ifLock = QCheckBox(self.centralwidget)self.ifLock.setObjectName(u"ifLock")self.ifLock.setGeometry(QRect(70, 169, 121, 20))self.ifLock.setMouseTracking(True)self.ok = QPushButton(self.centralwidget)self.ok.setObjectName(u"ok")self.ok.setGeometry(QRect(90, 200, 71, 23))self.splitter = QSplitter(self.centralwidget)self.splitter.setObjectName(u"splitter")self.splitter.setGeometry(QRect(61, 50, 161, 21))self.splitter.setOrientation(Qt.Horizontal)self.label_3 = QLabel(self.splitter)self.label_3.setObjectName(u"label_3")self.splitter.addWidget(self.label_3)self.workTime = QSpinBox(self.splitter)self.workTime.setObjectName(u"workTime")self.splitter.addWidget(self.workTime)self.label_5 = QLabel(self.splitter)self.label_5.setObjectName(u"label_5")self.splitter.addWidget(self.label_5)self.splitter_2 = QSplitter(self.centralwidget)self.splitter_2.setObjectName(u"splitter_2")self.splitter_2.setGeometry(QRect(61, 79, 161, 21))self.splitter_2.setOrientation(Qt.Horizontal)self.label_4 = QLabel(self.splitter_2)self.label_4.setObjectName(u"label_4")self.splitter_2.addWidget(self.label_4)self.restTime = QSpinBox(self.splitter_2)self.restTime.setObjectName(u"restTime")self.splitter_2.addWidget(self.restTime)self.label_6 = QLabel(self.splitter_2)self.label_6.setObjectName(u"label_6")self.splitter_2.addWidget(self.label_6)self.widget = QWidget(self.centralwidget)self.widget.setObjectName(u"widget")self.widget.setGeometry(QRect(60, 239, 126, 36))self.horizontalLayout = QHBoxLayout(self.widget)self.horizontalLayout.setObjectName(u"horizontalLayout")self.horizontalLayout.setContentsMargins(0, 0, 0, 0)self.label_7 = QLabel(self.widget)self.label_7.setObjectName(u"label_7")font1 = QFont()font1.setPointSize(18)font1.setBold(True)self.label_7.setFont(font1)self.horizontalLayout.addWidget(self.label_7)self.time = QLCDNumber(self.widget)self.time.setObjectName(u"time")self.horizontalLayout.addWidget(self.time)MainWindow.setCentralWidget(self.centralwidget)self.retranslateUi(MainWindow)QMetaObject.connectSlotsByName(MainWindow)# setupUidef retranslateUi(self, MainWindow):MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u4e45\u5750\u63d0\u9192", None))self.label_2.setText(QCoreApplication.translate("MainWindow", u"\u9009\u9879\uff1a", None))self.label.setText(QCoreApplication.translate("MainWindow", u"\u8ba1\u65f6\u5668\uff1a", None))self.ifLock.setText(QCoreApplication.translate("MainWindow", u"\u4f11\u606f\u65f6\u64ad\u653e\u97f3\u4e50", None))self.ok.setText(QCoreApplication.translate("MainWindow", u"\u5f00\u59cb", None))self.label_3.setText(QCoreApplication.translate("MainWindow", u"\u5de5\u4f5c\u65f6\u95f4\uff1a", None))self.label_5.setText(QCoreApplication.translate("MainWindow", u"\uff08\u5206\u949f\uff09", None))self.label_4.setText(QCoreApplication.translate("MainWindow", u"\u4f11\u606f\u65f6\u95f4\uff1a", None))self.label_6.setText(QCoreApplication.translate("MainWindow", u"\uff08\u5206\u949f\uff09", None))self.label_7.setText(QCoreApplication.translate("MainWindow", u"\u5de5\u4f5c", None))# retranslateUi
-
- 编写main.c文件。
-
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QLCDNumber from ui_SedentaryReminder import Ui_MainWindow from PySide6.QtCore import QTimer, Qt, QCoreApplication import pygameclass MainWindow(QMainWindow):def __init__(self):super(MainWindow, self).__init__()self.ui = Ui_MainWindow() # UI类的实例化self.ui.setupUi(self)# 初始时间self.workTime = 60self.restTime = 5self.total_seconds = self.workTime * 60self.ui.workTime.setValue(self.workTime) # 初始工作时间self.ui.restTime.setValue(self.restTime) # 初始休息时间# 音乐地址self.musicFilePath = "./music/music.mp3"# 初始化倒计时显示self.ui.time.setSegmentStyle(QLCDNumber.Filled)self.ui.time.setDigitCount(5) # 显示5位self.ui.time.display("00:00")# 实例化时间控件self.timer = QTimer(self)self.timer.setInterval(1000)# 初始化pygamepygame.init()pygame.mixer.init()# 点击确定开始运行self.ui.ok.clicked.connect(self.setTime)# 设置工作时间def setTime(self):self.workTime = int(self.ui.workTime.text())self.restTime = int(self.ui.restTime.text())self.total_seconds = self.workTime * 60if not self.timer.isActive():self.timer.start()self.timer.timeout.connect(self.countdownWork)# 设置工作倒计时def countdownWork(self):# 工作倒计时if self.total_seconds > 0:self.total_seconds -= 1minutes, seconds = divmod(self.total_seconds, 60)self.ui.time.display(f"{minutes:02d}:{seconds:02d}")else:self.timer.stop()self.timer.timeout.disconnect(self.countdownWork)# 休息设置self.total_seconds = self.restTime * 60self.ui.label_7.setText('休息')self.ui.ok.setDisabled(True)self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool) # 窗体置顶,无边框,在任务栏不显示图标window.show() # 重新显示# 播放音乐if self.ui.ifLock.isChecked():pygame.mixer.music.load(self.musicFilePath)pygame.mixer.music.play()self.timer.start() # 重新计时self.timer.timeout.connect(self.countdownRest)# 设置休息倒计时def countdownRest(self):# 休息倒计时if self.total_seconds > 0:self.total_seconds -= 1minutes, seconds = divmod(self.total_seconds, 60)self.ui.time.display(f"{minutes:02d}:{seconds:02d}")else:self.setWindowFlags(Qt.Window) # 重置窗口window.show() # 重新显示self.total_seconds = self.workTime * 60self.ui.label_7.setText('工作')self.ui.ok.setDisabled(False)self.timer.stop()self.timer.timeout.disconnect(self.countdownRest)# 关闭音乐pygame.mixer.music.stop()pygame.mixer.quit()pygame.quit()def closeEvent(self, event):super(MainWindow, self).closeEvent(event)sys.exit(app.exec())if __name__ == '__main__':app = QApplication([]) # 启动一个应用window = MainWindow() # 实例化主窗口window.show() # 展示主窗口sys.exit(app.exec()) # 应用关闭时关闭进程
-
4、展示
- 运行程序。
- 设置工作时间和休息时间,勾选播放音乐,点击开始。倒计时开始...
- 工作时间结束,播放音乐,开始休息倒计时。此时,窗口置顶,并且不能关闭,也不能点击开始。
- 休息时间结束,恢复窗口。此时可以关闭,也可以重新开始计时。
这篇关于Python项目——久坐提醒定时器(PySide6)编写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!