继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

做一个小闹钟,按规划做事...

OverWrite_235
关注TA
已关注
手记 138
粉丝 10
获赞 65

通过PyQt5实现设置一个小闹钟的功能,到了设置的时间后可以响起一段音乐来提醒。

file

需要小闹钟完整源代码,到文末获取下载链接。

导入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)

小闹钟实现的主要代码块就是上面这些了。

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP