Python利用PyQT5設(shè)置鬧鐘功能
通過(guò)PyQt5實(shí)現(xiàn)設(shè)置一個(gè)小鬧鐘的功能,到了設(shè)置的時(shí)間后可以響起一段音樂(lè)來(lái)提醒。
導(dǎo)入U(xiǎn)I界面組件相關(guān)的模塊
from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import *
導(dǎo)入應(yīng)用操作相關(guān)的模塊
import sys from PyQt5.QtMultimedia import *
初始化函數(shù) init_ui() 函數(shù),PyQt5 界面布局使用了三種,分別是垂直化布局、水平化布局、柵格布局。
def init_ui(self): self.setWindowTitle("小鬧鐘") # 設(shè)置應(yīng)用標(biāo)題 self.setWindowIcon(QIcon('clock.ico')) # 設(shè)置應(yīng)用圖標(biāo) form = QFormLayout() # 初始化一個(gè)表單布局 self.current_date_label = QLabel() self.current_date_label.setText("當(dāng)前時(shí)間:") 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("定時(shí)時(shí)間:") 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("設(shè)置鈴聲:") self.set_rightone_box = QComboBox() self.set_rightone_box.addItems(["冷漠 - 一路向北 (DJ版)","大城 - 下雪哈爾濱","許巍 - 時(shí)光"]) 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("公眾號(hào):[Python 集中營(yíng)]") self.start_btn = QPushButton() self.start_btn.setText("開(kāi)始") 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) # 設(shè)置主布局
創(chuàng)建槽函數(shù) show_current(),用于實(shí)時(shí)顯示時(shí)間的變化并將時(shí)間更新到QLabel組件上面,目前做的是秒級(jí)的時(shí)間更新。
def show_current(self): ''' 刷新當(dāng)前時(shí)間顯示、每隔一秒鐘刷新 :return: ''' current_time = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd') self.current_date_label_time.setText(current_time)
創(chuàng)建槽函數(shù) timing_his(),監(jiān)聽(tīng)定時(shí)時(shí)間是否到達(dá)。在定時(shí)時(shí)間到達(dá)時(shí)播放音樂(lè),現(xiàn)在代碼塊中總共引入了三首歌曲,需要的可以按照自己喜好添加自己喜歡的歌曲。
def timing_lis(self): if QDateTime.currentDateTime() < self.timing_date_time.dateTime(): print("[{}]:定時(shí)時(shí)間沒(méi)有到達(dá)".format(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd'))) else: print("[{}]:定時(shí)時(shí)間已經(jīng)到達(dá)".format(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd'))) self.current_timer_lis.stop() selected = self.set_rightone_box.currentText() print("開(kāi)始播放音樂(lè):{}".format(selected)) url = QUrl.fromLocalFile("{}.mp3".format(selected)) self.player.setMedia(QMediaContent(url)) self.player.play()
創(chuàng)建槽函數(shù) start_btn_click(),將該函數(shù)綁定開(kāi)始按鈕上用于啟動(dòng)鬧鐘。
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)
小鬧鐘實(shí)現(xiàn)的主要代碼塊就是上面這些了。
補(bǔ)充
還可以不利用PyQT5,直接用Python實(shí)現(xiàn)鬧鐘功能,示例代碼如下
音頻文件放入和.py文件同級(jí)的目錄下
import winsound # 導(dǎo)入此模塊實(shí)現(xiàn)聲音播放功能 import time # 導(dǎo)入此模塊,獲取當(dāng)前時(shí)間 # 提示用戶設(shè)置時(shí)間和分鐘 my_hour = input("請(qǐng)輸入時(shí):") my_minute = input("請(qǐng)輸入分:") flag = 1 while flag: t = time.localtime() # 當(dāng)前時(shí)間的紀(jì)元值 fmt = "%H %M" now = time.strftime(fmt, t) # 將紀(jì)元值轉(zhuǎn)化為包含時(shí)、分的字符串 now = now.split(' ') #以空格切割,將時(shí)、分放入名為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
到此這篇關(guān)于Python利用PyQT5設(shè)置鬧鐘功能的文章就介紹到這了,更多相關(guān)Python PyQT5鬧鐘功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django框架登錄加上驗(yàn)證碼校驗(yàn)實(shí)現(xiàn)驗(yàn)證功能示例
這篇文章主要介紹了Django框架登錄加上驗(yàn)證碼校驗(yàn)實(shí)現(xiàn)驗(yàn)證功能,結(jié)合實(shí)例形式分析了Django框架基于Pillow模塊的圖形驗(yàn)證碼生成與使用相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Python偽代碼分析點(diǎn)贊器實(shí)現(xiàn)原理及代碼
這篇文章主要介紹了Python偽代碼分析點(diǎn)贊器實(shí)現(xiàn)原理,本次點(diǎn)贊?rùn)C(jī)器人,主要面向電腦上的 Web 站點(diǎn),不涉及 APP 端,需要的朋友可以參考下2022-04-04Elasticsearch py客戶端庫(kù)安裝及使用方法解析
這篇文章主要介紹了Elasticsearch py客戶端庫(kù)安裝及使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Python numpy和matlab的幾點(diǎn)差異介紹
這篇文章主要介紹了Python numpy和matlab的幾點(diǎn)差異,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Pycharm最全報(bào)錯(cuò)的原因與解決方法總結(jié)(推薦!)
這篇文章主要給大家介紹了關(guān)于Pycharm最全報(bào)錯(cuò)的原因與解決方法的相關(guān)資料,文中記錄了Python各種報(bào)錯(cuò)解釋及處理方法,屬于個(gè)人記錄型,需要的朋友可以參考下2022-07-07