如何用Python做一个闹钟

简介: 如何用Python做一个闹钟

首先,老规矩,第一行一定是:

coding:utf-8

然后,我们要导入所需的模块:

复制代码
import datetime
import pygame
import os
import time
import random
from pynput.keyboard import Key,Listener
复制代码
本次所需的模块不多,由于都是我原先安装好的,所以我也并不确定它们是否要用pip安装。当然,使用PyCharm进行编程可以自动为你安装你使用到的未安装的模块。

复制代码
all_songs=[]
all_songs_dict={}
for root, dirs, names in os.walk("d:/Users/{}/Music".format(os.getlogin())):
for filename in names:
if os.path.join(root, filename)[-4:] == ".mp3" or os.path.join(root, filename)[-4:] == ".ogg":
all_songs.append(os.path.join(root, filename)[os.path.join(root, filename).find("\") + 1:-4])
all_songs_dict[
os.path.join(root, filename)[os.path.join(root, filename).find("\") + 1:-4]] = os.path.join(root,
filename).replace(
"\", "/")
复制代码
首先,我们需要获取电脑中的音乐。我们用os.walk获取Music文件夹中的所有ogg和mp3文件(这是由于我播放音乐使用的是Pygame,它似乎只支持这两种文件的音乐)。同时,通过对路径字符串的文字处理,提取出音乐名称存储入列表all_songs,并将其和原先的路径对应存入字典all_songs_dict。这时存入这两个变量中的音乐信息相当于一种保险机制,可以确保在找不到铃声文件时有备用铃声可以使用(当然,如果你的音乐文件夹是空的,这当然没用;但我不认为你的文件夹中会没有音乐)。

复制代码
finding = open("alarm_list.txt", "a")
finding.close()
finding = open("alarm_list.txt", "r")
alarm_list_undone = finding.readlines()
finding.close()
alarm_list = []
for i in alarm_list_undone:
alarm_list.append(i.replace("\n", ""))
复制代码
我们的闹钟时间当然需要存储在某个文件中,这样才能防止这个信息丢失。于是,我们打开了一个“alarm_list.txt”文件(为防止文件不存在,我们需要先用写入的模式打开一次文件,这样可以在文件不存在时创建;同时为防止这个操作清空文件,我们必须用追加写的“a”而不是“w”)。我们会读取文件中的内容,并删除换行后存入alarm_list。我们不必忧心文件中有无法识别的内容,因为文件是通过另一个编辑程序写入的,那个程序会保证文件内容是这个程序所能识别的。

复制代码
finding = open("dates_one.txt", "a")
finding.close()
finding = open("dates_one.txt", "r")
dates_undone_one = finding.readlines()
finding.close()

//代码效果参考:https://v.youku.com/v_show/id_XNjQwMDM5NTU2NA==.html
finding = open("dates_two.txt", "a")
finding.close()
finding = open("dates_two.txt", "r")
dates_undone_two = finding.readlines()
finding.close()
dates = {}
for i in range(len(dates_undone_one)):
dates[dates_undone_one[i].replace("\n", "")] =eval( dates_undone_two[i].replace("\n", ""))
复制代码
同理,对“dates_one.txt”和“dates_two.txt”也需要类似的处理。当然,因为文件中存储有表格,我们有必要用eval()函数回复它的表格类型。第二个不同在于这两个文件的内容会被存入一个字典,而不是表格。

复制代码
finding = open("songs_one.txt", "a")
finding.close()
finding = open("songs_one.txt", "r")
songs_undone_one = finding.readlines()
finding.close()
finding = open("songs_two.txt", "a")
finding.close()
finding = open("songs_two.txt", "r")
songs_undone_two = finding.readlines()
finding.close()
songs = {}
for i in range(len(songs_undone_two)):
try:
open(songs_undone_two[i].replace("\n", ""),"r")
except:
songs_undone_two[i]=all_songs_dict[all_songs[random.randint(0,len(all_songs)-1)]]+"\n"
with open("songs_two.txt", "w")as finding:
finding.writelines(songs_undone_two)
for i in range(len(songs_undone_one)):
songs[songs_undone_one[i].replace("\n", "")] = songs_undone_two[i].replace("\n", "")
复制代码
在对“songs_one.txt”和“songs_two.txt”的处理中,我们就需要用到表格all_songs了。为保证音乐都存在,它会尝试打开路径所指向的文件。如果找不到文件,它会将音乐修改为all_songs中所记录的随机一个音乐(在这里可以进行改善:加一个if判断,如果路径指向的文件后缀不是“.mp3”或“.ogg”,则也要修改音乐。这个工作感兴趣的读者可以自行加上,在此我不再给出代码——这主要是由于我在解释我的程序代码时即使发现了不合理之处,只要不是大问题,就也不愿再行修改的习惯)。

复制代码
finding = open("alarm_list.txt", "r")
finder=finding.read()
try:
while finder[-1] == "\n":
finder = finder[:-1]
except:
while False:
print()
with open("alarm_list.txt", "w") as finding:
finding.write(finder)
finding = open("dates_one.txt", "r")
finder=finding.read()
try:
while finder[-1] == "\n":
finder = finder[:-1]
except:
while False:
print()
with open("dates_one.txt", "w") as finding:
finding.write(finder)
finding = open("dates_two.txt", "r")
finder=finding.read()
try:
while finder[-1] == "\n":
finder = finder[:-1]
except:
while False:
print()
with open("dates_two.txt", "w") as finding:
finding.write(finder)

finding = open("songs_one.txt", "r")
finder=finding.read()
try:
while finder[-1] == "\n":
finder = finder[:-1]
except:
while False:
print()
with open("songs_one.txt", "w") as finding:
finding.write(finder)
finding = open("songs_two.txt", "r")
finder=finding.read()
try:

//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg0ODkyMA==.html
while finder[-1] == "\n":
finder = finder[:-1]
except:
while False:
print()
with open("songs_two.txt", "w") as finding:
finding.write(finder)
复制代码
我的删除闹钟的程序片段不知为何,一直有一些BUG,会在删除处于结尾的闹钟时留下空行(这会导致程序运行出错)。于是,我们很有必要对其的后果作出一些挽回:将每一个文件结尾的换行符删除。而这个程序就是进行了这样一个工作。

复制代码
def delete(i):
global all_songs, all_songs_dict, alarm_list, dates, songs,alarm_list_undone,dates_undone_one,dates_undone_two,songs_undone_one,songs_undone_two
try:
try:
alarm_list_undone.remove(i)
alarm_list[-1] = alarm_list[-1].replace("\n", "")
except:
alarm_list_undone.remove(i + "\n")
except:
if False:
print()
alarm_list.remove(i)
with open("alarm_list.txt", "w") as finding:
finding.writelines(alarm_list_undone)
for j in range(len(songs_undone_one)):
if songs_undone_one[j] == i or songs_undone_one[j] == i + "\n":
songs_undone_one.pop(j)
songs_undone_two.pop(j)
try:
songs_undone_one[-1]=songs_undone_one[-1].replace("\n", "")
songs_undone_two[-1]=songs_undone_two[-1].replace("\n", "")
except:
if False:
print()
songs = {}
for j in range(len(songs_undone_one)):
songs[songs_undone_one[j].replace("\n", "")] = songs_undone_two[j].replace("\n", "")
with open("songs_one.txt", "w") as finding:
finding.writelines(songs_undone_one)
with open("songs_two.txt", "w") as finding:
finding.writelines(songs_undone_two)
for j in range(len(dates_undone_one)):
if dates_undone_one[j] == i or dates_undone_one[j] == i + "\n":
dates_undone_one.pop(j)
dates_undone_two.pop(j)
try:
dates_undone_one[-1]=dates_undone_one[-1].replace("\n", "")
dates_undone_two[-1]=dates_undone_two[-1].replace("\n", "")
except:
if False:
print()
dates = {}
for j in range(len(dates_undone_one)):
dates[dates_undone_one[j].replace("\n", "")] = dates_undone_two[j].replace("\n", "")
with open("dates_one.txt", "w") as finding:
finding.writelines(dates_undone_one)
with open("dates_two.txt", "w") as finding:
finding.writelines(dates_undone_two)
复制代码

相关文章
|
2月前
|
Python
Python 闹钟程序
Python 闹钟程序
53 2
|
2月前
|
存储 Python
Python实战项目(十三)使用 Tkinter GUI 库构建闹钟应用程序
Python实战项目(十三)使用 Tkinter GUI 库构建闹钟应用程序
50 0
|
2月前
|
调度 Python
Python TK实现的闹钟
Python TK实现的闹钟
21 0
|
Python
python-ui之tkinter初步学习-制作自动更新时间的闹钟
python-ui之tkinter初步学习-制作自动更新时间的闹钟
125 0
|
11小时前
|
存储 数据可视化 API
Python 金融编程第二版(GPT 重译)(三)(5)
Python 金融编程第二版(GPT 重译)(三)
7 0
|
11小时前
|
数据可视化 Python
Python 金融编程第二版(GPT 重译)(三)(4)
Python 金融编程第二版(GPT 重译)(三)
11 2
|
11小时前
|
数据挖掘 索引 Python
Python 金融编程第二版(GPT 重译)(二)(5)
Python 金融编程第二版(GPT 重译)(二)
6 0
|
11小时前
|
索引 Python
Python 金融编程第二版(GPT 重译)(二)(4)
Python 金融编程第二版(GPT 重译)(二)
7 0
|
11小时前
|
存储 SQL 数据可视化
Python 金融编程第二版(GPT 重译)(二)(3)
Python 金融编程第二版(GPT 重译)(二)
8 0
|
2天前
|
大数据 程序员 Python
Python数据类型大变身!掌握列表推导式与生成器,编程效率翻倍不是梦
【7月更文挑战第2天】在Python中,列表推导式和生成器是提升效率的利器。列表推导式以简洁方式处理循环和条件,如将偶数平方化简为一行代码,提高代码可读性。生成器则按需生成数据,减少内存占用,适合处理大数据。通过`yield`函数实现惰性求值,有效避免内存溢出。掌握这两者,能优化Python编程体验。