Python利用PyQT5设置闹钟功能
作者:Python?集中营 时间:2023-05-08 15:34:08
通过PyQt5实现设置一个小闹钟的功能,到了设置的时间后可以响起一段音乐来提醒。
导入UI界面组件相关的模块
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
导入应用操作相关的模块
import sys
from PyQt5.QtMultimedia import *
初始化函数 init_ui() 函数,PyQt5 界面布局使用了三种,分别是垂直化布局、水平化布局、栅格布局。
def init_ui(self):
self.setWindowTitle("小闹钟") # 设置应用标题
self.setWindowIcon(QIcon('clock.ico')) # 设置应用图标
form = QFormLayout() # 初始化一个表单布局
self.current_date_label = QLabel()
self.current_date_label.setText("当前时间:")
self.current_date_label_time = QLabel()
self.current_date_label_time.setText(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd'))
self.current_timer = QTimer()
self.current_timer.timeout.connect(self.show_current)
self.current_timer.start(1000)
self.timing_date_label = QLabel()
self.timing_date_label.setText("定时时间:")
self.timing_date_time = QDateTimeEdit()
self.timing_date_time.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
self.timing_date_time.setDateTime(QDateTime.currentDateTime())
self.set_rightone_label = QLabel()
self.set_rightone_label.setText("设置铃声:")
self.set_rightone_box = QComboBox()
self.set_rightone_box.addItems(["冷漠 - 一路向北 (DJ版)","大城 - 下雪哈尔滨","许巍 - 时光"])
form.addRow(self.current_date_label,self.current_date_label_time)
form.addRow(self.timing_date_label,self.timing_date_time)
form.addRow(self.set_rightone_label,self.set_rightone_box)
hbox = QHBoxLayout() # 初始化水平布局
self.version = QLabel()
self.version.setText("公众号:[Python 集中营]")
self.start_btn = QPushButton()
self.start_btn.setText("开始")
self.start_btn.clicked.connect(self.start_btn_click)
hbox.addWidget(self.version)
hbox.addWidget(self.start_btn)
vbox = QVBoxLayout() # 初始化垂直布局
vbox.addLayout(form)
vbox.addLayout(hbox)
self.setLayout(vbox) # 设置主布局
创建槽函数 show_current(),用于实时显示时间的变化并将时间更新到QLabel组件上面,目前做的是秒级的时间更新。
def show_current(self):
'''
刷新当前时间显示、每隔一秒钟刷新
:return:
'''
current_time = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd')
self.current_date_label_time.setText(current_time)
创建槽函数 timing_his(),监听定时时间是否到达。在定时时间到达时播放音乐,现在代码块中总共引入了三首歌曲,需要的可以按照自己喜好添加自己喜欢的歌曲。
def timing_lis(self):
if QDateTime.currentDateTime() < self.timing_date_time.dateTime():
print("[{}]:定时时间没有到达".format(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd')))
else:
print("[{}]:定时时间已经到达".format(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd')))
self.current_timer_lis.stop()
selected = self.set_rightone_box.currentText()
print("开始播放音乐:{}".format(selected))
url = QUrl.fromLocalFile("{}.mp3".format(selected))
self.player.setMedia(QMediaContent(url))
self.player.play()
创建槽函数 start_btn_click(),将该函数绑定开始按钮上用于启动闹钟。
def start_btn_click(self):
self.current_timer_lis = QTimer()
self.current_timer_lis.timeout.connect(self.timing_lis)
self.current_timer_lis.start(500)
小闹钟实现的主要代码块就是上面这些了。
补充
还可以不利用PyQT5,直接用Python实现闹钟功能,示例代码如下
音频文件放入和.py文件同级的目录下
import winsound # 导入此模块实现声音播放功能
import time # 导入此模块,获取当前时间
# 提示用户设置时间和分钟
my_hour = input("请输入时:")
my_minute = input("请输入分:")
flag = 1
while flag:
t = time.localtime() # 当前时间的 * 值
fmt = "%H %M"
now = time.strftime(fmt, t) # 将 * 值转化为包含时、分的字符串
now = now.split(' ') #以空格切割,将时、分放入名为now的列表中
hour = now[0]
minute = now[1]
if hour == my_hour and minute == my_minute:
music = 'Good Time.wav'
winsound.PlaySound(music, winsound.SND_ALIAS)
flag = 0
来源:https://www.cnblogs.com/lwsbc/p/15786367.html
标签:Python,PyQT5,闹钟
0
投稿
猜你喜欢
使用Python编写简单的端口扫描器的实例分享
2023-07-20 20:18:32
MySQL实现字符串的拼接,截取,替换,查找位置的操作
2024-01-13 13:17:22
Request.ServerVariables各参数说明集合
2008-11-25 18:49:00
pytorch_detach 切断网络反传方式
2022-09-25 21:10:50
anaconda创建、查看、激活与删除虚拟环境指令总结
2023-04-09 13:01:23
python通过pil为png图片填充上背景颜色的方法
2021-03-01 07:32:38
Python实现单例模式的四种方式详解
2023-07-12 03:07:43
pandas loc与iloc用法及区别
2023-01-22 08:26:53
动态给表添加删除字段并同时修改它的插入更新存储过程
2011-12-01 10:18:28
如何使用Python自动生成报表并以邮件发送
2021-07-20 08:51:06
python爬虫之爬取百度音乐的实现方法
2022-09-02 06:22:10
Vue冷门技巧递归组件实践示例详解
2024-05-09 15:13:04
python 自定义异常和异常捕捉的方法
2022-09-10 12:06:31
javascript学习总结之js使用技巧
2024-04-18 10:59:16
Python新手入门webpy小应用开发
2021-02-03 18:53:59
WAP2.0网页设计中的交互细节
2010-08-12 20:50:00
mdf文件和ldf文件导入到sql server 2005实现语句
2024-01-21 14:38:07
关于代码阅读问题的小技巧 脚本之家原创(适合所有网站)不定时更新
2024-01-04 14:57:42
读取MySQL的log方法
2011-07-01 12:04:57
Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录
2023-04-13 19:29:37