使用python turtle绘制时钟
时间: 2023-10-24 12:07:36 浏览: 170
Sure, 我们可以使用Python的turtle库来绘制时钟。下面是一个基本的例子:
```
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()
```
希望你喜欢这个例子,如有其他问题,请继续咨询。
阅读全文