通过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

到此这篇关于python利用pyqt5设置闹钟功能的文章就介绍到这了,更多相关python pyqt5闹钟功能内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!