最近在抖音上看到一个非常有趣的时钟,但是几乎都是css,html和 js做的。想找一下python的源代码,可是网上都没有相关的资源,有一个用 turtle库做的运行时间长了会一顿一顿的,效果与抖音上刷到的差别较大。于是决定自己写一个,花了两三个晚上学习了一下python的pygame库来做这个事。成品(见下方样图)除了旋转效果与抖音上看到的不大一致之外,基本功能都实现了,现分享一下自己的代码,感兴趣的可以拷贝回去也玩一下!也可以关注我的个人公众号<生信白白>,回复“时钟”或者“罗盘”,看一下动态的视频效果。
1. 库的安装
首先打开pycharm,在pycharm的terminal中安装几个用到的库pygame和pyinstaller(将python代码打包成exe程序)这个库,如图:
pip3 install pygame pyinstaller -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
2. 代码测试
将下面的代码复制到pycharm中,运行以下,看看有没有报错,没有的话就继续
import math
import pygame
import sys
import tkinter
from datetime import datetime
tk = tkinter.Tk()
width = tk.winfo_screenwidth()
height = tk.winfo_screenheight()
tk.quit()
def print_text(font, x, y, text, angle, color=(255, 255, 255)):
"""定义一个用于输出指定位置和角度文本的函数"""
img_text = font.render(text, True, color)
img_text = pygame.transform.rotate(img_text, angle)
screen.blit(img_text, (x, y))
def cycle_text(cirText, bins, today_xx, cirRadius, font, color=(255, 255, 255)):
"""
定义一个输出循环文本的函数
cirText: 环形循环文本,如日期和时间的中文
bins: 圆需要分为多少等分,如秒需要分成60等分
today_xx: 接收当前时间(月日周时分秒)的具体数值
cirRadius: 指定环形文本的半径
font: 指定使用的字体
"""
for i in range(1, len(cirText) + 1):
# c_angle: 旋转一次的角度
c_angle = math.radians(360 / bins) * (today_xx - i)
# t_angle: 环上每个独立文本的角度
t_angle = 0 - (360 / bins) * (today_xx - i)
# add_x: 环上每个独立文本的横坐标距离pos_x的距离
add_x = math.cos(c_angle) * cirRadius
# add_x: 环上每个独立文本的横坐标距离pos_x的距离
add_y = math.sin(c_angle) * cirRadius
# print_text(): 调用上面定义的函数,输出换上每个文本
print_text(font, pos_x + add_x, pos_y + add_y, str(cirText[i - 1]), angle=t_angle, color=color)
# 初始化一个界面
pygame.init()
# 屏幕大小
screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
# 标题
pygame.display.set_caption("Python Clock")
# 月,日,时,分,秒字体
font1 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 16)
# 年字体
font2 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 25)
# 周字体
font3 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 22)
# 时钟中心
pos_x = width / 2
pos_y = (height / 2) - 30
r = 395
# 年月日周时分秒对应中文字符
secondsText = ["零壹秒", "零贰秒", "零叁秒", "零肆秒", "零伍秒", "零陆秒", "零柒秒", "零捌秒", "零玖秒", "壹拾秒", "拾壹秒", "拾贰秒", "拾叁秒",
"拾肆秒", "拾伍秒", "拾陆秒", "拾柒秒", "拾捌秒", "拾玖秒", "贰拾秒", "贰拾壹秒", "贰拾贰秒", "贰拾叁秒", "贰拾肆秒", "贰拾伍秒",
"贰拾陆秒", "贰拾柒秒", "贰拾捌秒", "贰拾玖秒", "叁拾秒", "叁拾壹秒", "叁拾贰秒", "叁拾叁秒", "叁拾肆秒", "叁拾伍秒", "叁拾陆秒",
"叁拾柒秒", "叁拾捌秒", "叁拾玖秒", "肆拾秒", "肆拾壹秒", "肆拾贰秒", "肆拾叁秒", "肆拾肆秒", "肆拾伍秒", "肆拾陆秒", "肆拾柒秒",
"肆拾捌秒", "肆拾玖秒", "伍拾秒", "伍拾壹秒", "伍拾贰秒", "伍拾叁秒", "伍拾肆秒", "伍拾伍秒", "伍拾陆秒", "伍拾柒秒", "伍拾捌秒",
"伍拾玖秒", "零 秒"]
minuteText = ["零壹分", "零贰分", "零叁分", "零肆分", "零伍分", "零陆分", "零柒分", "零捌分", "零玖分", "壹拾分", "拾壹分", "拾贰分", "拾叁分",
"拾肆分", "拾伍分", "拾陆分", "拾柒分", "拾捌分", "拾玖分", "贰拾分", "贰拾壹分", "贰拾贰分", "贰拾叁分", "贰拾肆分", "贰拾伍分",
"贰拾陆分", "贰拾柒分", "贰拾捌分", "贰拾玖分", "叁拾分", "叁拾壹分", "叁拾贰分", "叁拾叁分", "叁拾肆分", "叁拾伍分", "叁拾陆分",
"叁拾柒分", "叁拾捌分", "叁拾玖分", "肆拾分", "肆拾壹分", "肆拾贰分", "肆拾叁分", "肆拾肆分", "肆拾伍分", "肆拾陆分", "肆拾柒分",
"肆拾捌分", "肆拾玖分", "伍拾分", "伍拾壹分", "伍拾贰分", "伍拾叁分", "伍拾肆分", "伍拾伍分", "伍拾陆分", "伍拾柒分", "伍拾捌分",
"伍拾玖分", "零 分"]
hourText = ["零壹点", "零两点", "零叁点", "零肆点", "零伍点", "零陆点", "零柒点", "零捌点", "零玖点", "壹拾点", "拾壹点", "拾贰点",
"拾叁点", "拾肆点", "拾伍点", "拾陆点", "拾柒点", "拾捌点", "拾玖点", "贰拾点", "贰拾壹点", "贰拾贰点", "贰拾叁点", "零 点"]
weekText = ["星期壹", "星期贰", "星期叁", "星期肆", "星期伍", "星期陆", "星期日"]
dayText = ["零壹日", "零贰日", "零叁日", "零肆日", "零伍日", "零陆日", "零柒日", "零捌日", "零玖日", "壹拾日", "拾壹日", "拾贰日",
"拾叁日", "拾肆日", "拾伍日", "拾陆日", "拾柒日", "拾捌日", "拾玖日", "贰拾日", "贰拾壹日", "贰拾贰日", "贰拾叁日",
"贰拾肆日", "贰拾伍日", "贰拾陆日", "贰拾柒日", "贰拾捌日", "贰拾玖日", "叁拾日", "叁拾壹日"]
monthText = ["零壹月", "零贰月", "零叁月", "零肆月", "零伍月", "零陆月", "零柒月", "零捌月", "零玖月", "拾 月", "拾壹月", "拾贰月"]
# 为了更方便的提取年对应的中文字符,先自动生成一个数字列表,将第2020-2050个元素更换为2020年-2050年对应的中分字符
yearText = list(range(1, 3000))
yearText[2020:2051] = ["贰零贰零年", "贰零贰壹年", "贰零贰贰年", "贰零贰叁年", "贰零贰肆年", "贰零贰伍年", "贰零贰陆年", "贰零贰柒年",
"贰零贰捌年", "贰零贰玖年", "贰零叁零年", "贰零叁壹年", "贰零叁贰年", "贰零叁叁年", "贰零叁肆年", "贰零叁伍年",
"贰零叁陆年", "贰零叁柒年", "贰零叁捌年", "贰零叁玖年", "贰零肆零年", "贰零肆壹年", "贰零肆贰年", "贰零肆叁年",
"贰零肆肆年", "贰零肆伍年", "贰零肆陆年", "贰零肆柒年", "贰零肆捌年", "贰零肆玖年", "贰零伍零年"]
while True:
# 鼠标点击x或按下键盘esc键时退出
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
sys.exit()
# 填充背景颜色
screen.fill(color=(0, 115, 120))
# screen.fill(color=(0, 0, 0))
# 绘制一条表明当前日期时间的红线
# pygame.draw.circle(screen, color="red", center=(pos_x, pos_x), radius=10, width=5,)
pygame.draw.line(screen, color="red", start_pos=(pos_x+15, pos_y + 25), end_pos=(pos_x+r+60, pos_y + 25), width=2)
# 获取当前年份
year = datetime.today().year
# 输出当前年份
print_text(font2, pos_x - 35, pos_y - 15, text=yearText[year], angle=0, color="red")
# 获取当前月份
months = datetime.today().month
# 输出月环(会根据月份的变化,顺时针方向旋转,红线处为当前月份)
cycle_text(cirText=monthText, bins=12, today_xx=months, cirRadius=r - 290, font=font1, color="white")
# 天数这里需要考虑闰年和非闰年以及每月天数不一致的情况
days = datetime.today().day
if months in [1, 3, 5, 7, 8, 10, 12]:
cycle_text(cirText=dayText, bins=31, today_xx=days, cirRadius=r - 225, font=font1, color="white")
elif months in [4, 6, 9, 11]:
cycle_text(cirText=dayText[0:-1], bins=30, today_xx=days, cirRadius=r - 225, font=font1, color="white")
elif months == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
'''如果是闰年'''
cycle_text(cirText=dayText[0:-2], bins=29, today_xx=days, cirRadius=r - 225, font=font1, color="white")
else:
'''如果是非闰年'''
cycle_text(cirText=dayText[0:-3], bins=28, today_xx=days, cirRadius=r - 225, font=font1, color="white")
# 以下周,时,分,秒与月环相似
weeks = datetime.today().isoweekday()
print_text(font3, pos_x - 8, pos_y + 30, text=weekText[weeks-1], angle=0, color="blue")
# cycle_text(cirText=weekText, bins=7, today_xx=weeks, cirRadius=r - 185, font=font1, color="white")
hours = datetime.today().hour % 24
cycle_text(cirText=hourText, bins=24, today_xx=hours, cirRadius=r - 150, font=font1, color="white")
minutes = datetime.today().minute
cycle_text(cirText=minuteText, bins=60, today_xx=minutes, cirRadius=r - 75, font=font1, color="white")
seconds = datetime.today().second
cycle_text(cirText=secondsText, bins=60, today_xx=seconds, cirRadius=r, font=font1, color="white")
# 最后别忘记刷新一下界面
pygame.display.update()
3. 安装程序打包(2022.02.26更)
使用pyinstaller打包成exe程序
pyinstaller -F -w ./main.py
这行命令运行完之后,在./目录下会多出两个文件夹和一个.spec的文件,如图:
打开./build文件夹,里面有一个.exe程序,该程序就是打包成的可执行程序(只要这个程序,其他生成的文件都是可以删除的)。将这个文件的拓展名由main.exe改成pythonClock.scr, 然后将它复制到C:\Windows\system32\目录下。
4. 设置屏幕保护(Windows11)
桌面右键–个性化–锁屏界面–屏幕保护程序–弹出以下窗口;
在屏幕保护程序处选择pythonClock,应用确定即可(不过存在个BUG,退出屏保必需要按Esc,光动鼠标或者按键盘其他键无法退出这个界面,后续看看能不能解决这个问题!)。
如果不想折腾,也可以到我的公众号回复“屏保”下载pythonClock.scr文件
文章来源:https://www.toymoban.com/news/detail-423183.html
文章来源地址https://www.toymoban.com/news/detail-423183.html
到了这里,关于用python做一个抖音上很火的罗盘时钟(更新版,并将其做成屏保程序)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!