Python项目——久坐提醒定时器(PySide6)编写

2024-01-19 12:52

本文主要是介绍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)编写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/622494

相关文章

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl

Java调用Python的四种方法小结

《Java调用Python的四种方法小结》在现代开发中,结合不同编程语言的优势往往能达到事半功倍的效果,本文将详细介绍四种在Java中调用Python的方法,并推荐一种最常用且实用的方法,希望对大家有... 目录一、在Java类中直接执行python语句二、在Java中直接调用Python脚本三、使用Run

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp

PyQt5+Python-docx实现一键生成测试报告

《PyQt5+Python-docx实现一键生成测试报告》作为一名测试工程师,你是否经历过手动填写测试报告的痛苦,本文将用Python的PyQt5和python-docx库,打造一款测试报告一键生成工... 目录引言工具功能亮点工具设计思路1. 界面设计:PyQt5实现数据输入2. 文档生成:python-

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib