python 模拟数字时钟 图形化

img


可以教教我这个怎么做嘛?不会python图形化 ,题目是模拟数字时钟


可以使用turtle库来实现一个简单的数字时钟:
python
import turtle
import time

turtle.shape('turtle')
turtle.speed(10)

def draw_number(x, y, num):
    if num == 1:
        turtle.goto(x, y)
        turtle.pendown()
        turtle.goto(x + 50, y)
    elif num == 2:
        turtle.goto(x, y)
        turtle.pendown()
        turtle.goto(x + 50, y)
        turtle.goto(x + 25, y - 50)
        turtle.goto(x, y - 50)
        turtle.goto(x + 25, y - 50)
    # 省略其他数字的绘制逻辑...

def draw_clock():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.pendown()
    turtle.circle(200)

    turtle.penup()
    turtle.goto(0, 0)
    draw_number(-80, 0, 12)
    draw_number(-40, 0, 1)
    draw_number(0, 0, 2)
    draw_number(40, 0, 3)
    # 省略其他数字的绘制...

    sec_hand_len = 160
    min_hand_len = 140
    hour_hand_len = 100

    while True:
        sec = time.strftime('%S')
        min = time.strftime('%M')
        hour = time.strftime('%I')

        sec_angle = (int(sec) * 6) % 60
        min_angle = (int(min) * 6) 
        hour_angle = (int(hour) * 30) + (int(min) * 0.5)
        turtle.clear()
        draw_clock()

        turtle.penup()
        turtle.goto(0, 0)
        turtle.setheading(sec_angle)
        turtle.pendown()
        turtle.forward(sec_hand_len)

        # 绘制分针和时针...

        time.sleep(1)
        turtle.clear()

draw_clock()
这个程序使用turtle模块绘制一个圆形的时钟面,并在while循环中每秒更新时、分、秒针的角度并绘制对应的指针,实现一个简单的模拟时钟效果。