实现效果
介绍
本项目利用Python的turtle模块实现了一个动态时钟,包括秒、分、时三个表针的动态显示,并在时钟周围绘制了背景渐变色环和日期信息。通过这个项目,可以学习如何使用turtle模块绘制图形、管理多个表针的动画效果以及如何在GUI应用中实现动态更新。
环境配置
- Python版本: 3.x
- 依赖库: 无需额外安装第三方库,因为turtle是Python标准库的一部分。
项目分布
- set_gradient_color(turtle_obj, start_color, end_color, steps): 设置背景渐变色函数。
- move(distance): 悬空移动函数,用于移动turtle对象而不绘制轨迹。
- createHand(name, length, color): 创建表针turtle的函数。
- createClock(radius): 创建时钟的函数,包括背景渐变色环和刻度。
- getWeekday(today): 获取今天是星期几的函数。
- getDate(today): 获取今天日期的函数。
- startTick(second_hand, minute_hand, hour_hand, printer): 动态更新表针位置和日期显示的主函数。
- start(): 启动时钟的主函数,设置初始绘图环境和各表针对象,并开启动态更新。
详细代码
import turtle
import datetime
# 设置背景颜色和透明度
def set_gradient_color(turtle_obj, start_color, end_color, steps):
delta_r = (end_color[0] - start_color[0]) / steps
delta_g = (end_color[1] - start_color[1]) / steps
delta_b = (end_color[2] - start_color[2]) / steps
for i in range(steps):
r = start_color[0] + delta_r * i
g = start_color[1] + delta_g * i
b = start_color[2] + delta_b * i
turtle_obj.pencolor(r, g, b)
yield
# 悬空移动
def move(distance):
turtle.penup()
turtle.forward(distance)
turtle.pendown()
# 创建表针turtle
def createHand(name, length, color):
turtle.reset()
turtle.color(color)
move(-length * 0.01)
turtle.begin_poly()
turtle.forward(length * 1.01)
turtle.end_poly()
hand = turtle.get_poly()
turtle.register_shape(name, hand)
# 创建时钟
def createClock(radius):
turtle.reset()
turtle.pensize(7)
gradients = set_gradient_color(turtle, (1, 0, 0), (0, 0, 1), 60)
for i in range(60):
move(radius)
if i % 5 == 0:
turtle.forward(20)
move(-radius-20)
else:
turtle.dot(5)
move(-radius)
next(gradients)
turtle.right(6)
# 获得今天是星期几
def getWeekday(today):
return ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][today.weekday()]
# 获得今天的日期
def getDate(today):
return '%s年%s月%s日' % (today.year, today.month, today.day)
# 动态显示表针
def startTick(second_hand, minute_hand, hour_hand, printer):
today = datetime.datetime.today()
second = today.second + today.microsecond * 1e-6
minute = today.minute + second / 60.
hour = (today.hour + minute / 60) % 12
second_hand.setheading(6 * second)
minute_hand.setheading(6 * minute)
hour_hand.setheading(30 * hour)
turtle.tracer(False)
printer.clear()
printer.goto(0, 65)
printer.write(getWeekday(today), align='center', font=("Courier New", 16, "bold"))
printer.goto(0, -85)
printer.write(getDate(today), align='center', font=("Courier New", 16, "bold"))
turtle.tracer(True)
turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)
# 开始运行时钟
def start():
turtle.bgcolor("black")
turtle.tracer(False)
turtle.mode('logo')
createHand('second_hand', 150, "red")
createHand('minute_hand', 125, "white")
createHand('hour_hand', 85, "yellow")
second_hand = turtle.Turtle()
second_hand.shape('second_hand')
second_hand.shapesize(1, 1, 3)
second_hand.speed(0)
second_hand.color("white")
minute_hand = turtle.Turtle()
minute_hand.shape('minute_hand')
minute_hand.shapesize(1, 1, 3)
minute_hand.speed(0)
minute_hand.color("white")
hour_hand = turtle.Turtle()
hour_hand.shape('hour_hand')
hour_hand.shapesize(1, 1, 3)
hour_hand.speed(0)
hour_hand.color("white")
printer = turtle.Turtle()
printer.hideturtle()
printer.color("cyan")
printer.penup()
createClock(160)
turtle.tracer(True)
startTick(second_hand, minute_hand, hour_hand, printer)
# 添加炫目效果
def blink_hands():
for hand in [second_hand, minute_hand, hour_hand]:
hand.color("white")
turtle.ontimer(lambda: hand.color("red"), 500)
turtle.ontimer(lambda: hand.color("white"), 1000)
turtle.ontimer(blink_hands, 2000)
turtle.mainloop()
if __name__ == '__main__':
start()
总结
本项目通过turtle模块实现了一个动态时钟的绘制与显示,展示了Python在图形化界面设计中的应用能力。项目中涵盖了背景渐变色环的绘制、多个表针的动态更新、日期信息的显示等功能。通过学习此项目,可以更深入地理解Python中GUI编程的基本原理和实现方法。