python turtle库实现时钟

import turtle
import datetime

def skip(distance):
    """
    移动一段距离,不留下痕迹
    :param distance:像素
    :return:
    """
    turtle.penup()
    turtle.forward(distance)
    turtle.pendown()


def draw_clock():
    """画表盘"""
    # 让乌龟的朝向默认向上
    turtle.reset()
    turtle.mode('logo')
    for i in range(60):
        skip(160)
        # 根据5格一个时钟
        if i % 5 == 0:
            # 画时钟
            turtle.pensize(7)
            turtle.forward(20)
            if i == 0:
                turtle.write(12, align='center', font=('Courier',
                                                       14, 'bold'))
            elif i == 25 or i == 30 or i == 35:
                skip(25)
                turtle.write(int(i / 5), align='center', font=('Courier',
                                                               14, 'bold'))
                skip(-25)
            else:
                turtle.write(int(i / 5), align='center', font=('Courier',
                                                               14, 'bold'))

            skip(-20)
        else:
            turtle.pensize(1)
            turtle.dot()
        skip(-160)
        turtle.right(6)


def get_week(t):
    week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
    return week[t.weekday()]


def create_hand(length, name):
    skip(-length * 0.1)
    turtle.penup()
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    turtle.end_poly()
    turtle.home()
    # 注册
    turtle.register_shape(name, turtle.get_poly())
    hand = turtle.Turtle()
    hand.shape(name)
    hand.shapesize(1,1,3)
    return hand


def run():
    t = datetime.datetime.today()
    bob.penup()
    bob._tracer(False)
    bob.forward(65)
    bob.write(get_week(t), align='center', font=('Courier',
                                                    14, 'bold'))
    bob.back(130)
    bob.write(t.strftime('%Y-%m-%d'), align='center', font=('Courier',                                                         14, 'bold'))
    bob.home()
    bob._tracer(True)
    # 指针移动
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second/60
    hour = t.hour + minute/60
    # 设置转向
    second_hand.setheading(6*second)
    minute_hand.setheading(6*minute)
    hour_hand.setheading(30*hour)
    turtle.ontimer(run, 1000)







if __name__ == '__main__':
    # 画时针
    turtle.mode('logo')
    turtle.tracer(False)
    draw_clock()
    turtle.tracer(True)
    global  second_hand, minute_hand, hour_hand, bob
    second_hand = create_hand(135, "second_hand")
    minute_hand = create_hand(125, "minute_hand")
    hour_hand = create_hand(90, "hour_hand")
    bob = turtle.Turtle()
    bob.hideturtle()
    # 隐藏小乌龟
    run()
    turtle.mainloop()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, 我们可以使用Pythonturtle来绘制时钟。下面是一个基本的例子: ``` import turtle import time # Set up the turtle parameters wn = turtle.Screen() wn.bgcolor("black") wn.setup(width=600, height=600) wn.title("Analog Clock") wn.tracer(0) # Create the hour hand hour_hand = turtle.Turtle() hour_hand.speed(0) hour_hand.shape("arrow") hour_hand.color("white") hour_hand.shapesize(stretch_wid=4, stretch_len=10) hour_hand.penup() # Create the minute hand minute_hand = turtle.Turtle() minute_hand.speed(0) minute_hand.shape("arrow") minute_hand.color("white") minute_hand.shapesize(stretch_wid=6, stretch_len=15) minute_hand.penup() # Create the second hand second_hand = turtle.Turtle() second_hand.speed(0) second_hand.shape("arrow") second_hand.color("red") second_hand.shapesize(stretch_wid=2, stretch_len=20) second_hand.penup() # Define the function to update the time def update_time(): current_time = time.localtime() hour = current_time.tm_hour minute = current_time.tm_min second = current_time.tm_sec # Calculate the angle of the hands hour_angle = hour * 30 + minute / 2 minute_angle = minute * 6 second_angle = second * 6 # Update the hands' positions hour_hand.setheading(hour_angle) minute_hand.setheading(minute_angle) second_hand.setheading(second_angle) # Update the turtle screen wn.update() # Call the function again after 1000ms (1 second) wn.ontimer(update_time, 1000) # Call the function to start the clock update_time() turtle.done() ``` 希望你喜欢这个例子,如有其他问题,请继续咨询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值