IT评测·应用市场-qidao123.com
标题:
打造个性化跨年时钟:整点报时与祝福语、背景音乐播放的完美联合
[打印本页]
作者:
一给
时间:
2025-1-2 19:21
标题:
打造个性化跨年时钟:整点报时与祝福语、背景音乐播放的完美联合
打造个性化跨年时钟:整点报时与祝福语、背景音乐播放的完美联合
弁言
随着2025新年的临近,每个人都在等待着新的开始。为了给各人带来更加个性化的体验,我开发了一款独特的跨年时钟应用程序。这款时钟不但可以或许表现当前时间,还能根据不同的时间段主动调整背景颜色,并在特定的时间播放祝福语音。本文将具体先容怎样使用Python和Tkinter库实现这一功能。
功能概述
这款跨年时钟具备以下重要功能:
实时表现当前时间
:时钟会根据用户的本地时区(如上海)表现当前时间。
动态背景颜色变革
:白天(6:00-17:00)背景为白色,夜晚为玄色(但是今天是特殊日子换成紫色),增加视觉上的舒适感。
随机指针颜色变革
:每隔6到15秒,指针颜色会随机变革,增加意见意义性。
整点报时
:每小时整点或半点时,播放一段报时声音。
元旦祝福
:在新年第一天的前三秒内,表现“元旦快乐”并播放祝福语音。(用AI生成)
窗口始终置顶
:确保时钟始终处于屏幕最前端,方便查看。
背景音乐
:在报时程序开始第2秒开始运行,是另一个py程序里播放。
技术实现
环境准备
首先,确保安装了所需的库:
pip install pytz pygame
复制代码
代码解析
导入须要的库
import tkinter as tk
from tkinter import Canvas
import datetime
import time
import math
import pytz
import pygame # 用于播放声音
import threading # 用于多线程
import random # 用于随机选择颜色
复制代码
设置时区和颜色列表
timezone = pytz.timezone('Asia/Shanghai') # 这里可以改为任何你想要的时区
color_lst = ["white", "green", "blue", "yellow", "red", "orange"]
last_color_change_time = time.time()
current_color = random.choice(color_lst)
lock = threading.Lock()
复制代码
获取当前时间
def get_time():
"""获取当前时区的时间"""
now = datetime.datetime.now(timezone)
return now.hour, now.minute, now.second
复制代码
根据时间设置背景颜色
def white_black(h):
if 6 <= h < 17: # 白天(早上6:00到晚上6:00)
return 'white'
else: # 夜晚
return 'purple' # 平时是黑色的,今天特殊节日改成紫色
复制代码
绘制时钟
def draw_clock(canvas):
global last_color_change_time, current_color, bg_color
canvas.delete("all") # 清除画布
h, m, s = get_time()
with lock:
if time.time() - last_color_change_time >= random.randint(6, 15):
current_color = random.choice(color_lst)
last_color_change_time = time.time()
bg_color = white_black(h)
canvas.create_oval(75, 75, 525, 525, width=18, outline=current_color, fill=bg_color)
for i in range(60):
angle = math.pi / 30 * i - math.pi / 2
x1 = 300 + 210 * math.cos(angle)
y1 = 300 + 210 * math.sin(angle)
if i % 5 == 0:
x2 = 300 + 180 * math.cos(angle)
y2 = 300 + 180 * math.sin(angle)
text_x = 300 + 150 * math.cos(angle)
text_y = 300 + 150 * math.sin(angle)
canvas.create_text(text_x, text_y, text=str(i // 5 or 12), font=("Arial", 30), fill='black' if bg_color == 'white' else 'white')
else:
x2 = 300 + 195 * math.cos(angle)
y2 = 300 + 195 * math.sin(angle)
canvas.create_line(x1, y1, x2, y2, fill='black' if bg_color == 'white' else 'white')
hour_angle = (h % 12) * 30 + m / 2 - 90
hx = 300 + 105 * math.cos(math.radians(hour_angle))
hy = 300 + 105 * math.sin(math.radians(hour_angle))
canvas.create_line(300, 300, hx, hy, fill='blue', width=9)
minute_angle = (m * 6) - 90
mx = 300 + 150 * math.cos(math.radians(minute_angle))
my = 300 + 150 * math.sin(math.radians(minute_angle))
canvas.create_line(300, 300, mx, my, fill='blue', width=6)
second_angle = (s * 6) - 90
sx = 300 + 180 * math.cos(math.radians(second_angle))
sy = 300 + 180 * math.sin(math.radians(second_angle))
canvas.create_line(300, 300, sx, sy, fill="yellow", width=3)
canvas.create_oval(292, 292, 312, 312, fill='white', outline='black')
if h == 0 and m >= 0 and s >= 0:
text_x = 300
text_y = 230
canvas.create_text(text_x, text_y, text="元旦 快乐", font=("Arial", 36, "bold"), fill='yellow')
if h == 0 and m >= 0 and s >= 1 and s <= 3:
play_sound('/Users/liuxiaowei/Downloads/元旦快乐.mp3', 1) # 用AI生成
复制代码
播放声音
def play_sound(file_path, times):
try:
pygame.mixer.init()
sound = pygame.mixer.Sound(file_path)
for _ in range(times):
sound.play()
time.sleep(sound.get_length())
except Exception as e:
print(f"Error playing sound: {e}")
复制代码
更新时钟
def update_clock():
draw_clock(canvas)
h, m, s = get_time()
if m == 0 and s == 0:
threading.Thread(target=play_sound, args=("../chime.wav", h % 12 or 12)).start()
elif m == 30 and s == 0:
threading.Thread(target=play_sound, args=("../chime.wav", 1)).start()
root.after(1000, update_clock)
复制代码
创建主窗口
root = tk.Tk()
if datetime.datetime.now().year == 2025: # 判断年份为2025,窗口标题更改为2025
year1 = '20'
year2 = '25'
root.title(f"^ {year1} ⏰ {year2} ^")
root.attributes('-topmost', True)
root.resizable(True, True)
canvas = Canvas(root, width=600, height=600, highlightthickness=0)
canvas.pack()
update_clock()
root.mainloop()
复制代码
运行结果
播放背景音乐源码
import os
import time
from datetime import datetime
# 设置目标时间
target_time = "00:00:02" # 例如:下午2:30
# 定义播放音乐的函数
def play_music(song_name):
os.system(f"""osascript -e 'tell application "Music" to play "{song_name}"' """)
# 主循环,检查当前时间
while True:
current_time = datetime.now().strftime("%H:%M:%S")
if current_time == target_time:
play_music("/Users/liuxiaowei/Downloads/上海滩叶丽仪.mp3") # 替换为实际的歌曲名称
break
time.sleep(1) # 每分钟检查一次
复制代码
总结
通过上述代码,我们成功实现了一个功能丰富的跨年时钟。它不但可以或许在特定时间播放祝福语音,还可以或许根据时间和环境主动调整背景颜色和指针颜色,增加了用户体验的意见意义性和个性化。渴望这个项目能为各人的新年增加一份特别的惊喜和温馨。
欢迎点赞、关注、转发、收藏!!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4