Python用turtle模块绘制一个指针式时钟
时间: 2023-11-05 09:12:26 浏览: 99
python使用turtle库绘制时钟
5星 · 资源好评率100%
好的,下面是Python代码:
```python
import turtle
import time
# 画时钟刻度
def draw_clock(scale):
for i in range(12):
turtle.penup()
turtle.goto(0, 210)
turtle.setheading(-i * 30)
turtle.pendown()
turtle.forward(20)
if i == 0:
turtle.write('12', font=('Arial', 12, 'normal'))
elif i == 3:
turtle.write('3', font=('Arial', 12, 'normal'))
elif i == 6:
turtle.write('6', font=('Arial', 12, 'normal'))
elif i == 9:
turtle.write('9', font=('Arial', 12, 'normal'))
turtle.penup()
turtle.goto(0, 0)
for i in range(60):
turtle.pencolor('gray')
turtle.penup()
turtle.goto(0, 200)
turtle.setheading(-i * 6)
if i % 5 == 0:
turtle.pendown()
turtle.forward(10)
else:
turtle.pendown()
turtle.forward(5)
turtle.penup()
turtle.goto(0, 0)
turtle.speed('fastest')
# 画指针函数
def draw_hand(name, length, value):
turtle.penup()
turtle.goto(0, 0)
turtle.setheading(90 - value * (360 / name))
turtle.pendown()
turtle.pencolor('black')
turtle.pensize(2)
turtle.forward(length)
hour = int(time.strftime('%H'))
minute = int(time.strftime('%M'))
second = int(time.strftime('%S'))
draw_hand(12, 100, hour % 12 * 5 + minute / 12)
draw_hand(60, 140, minute)
draw_hand(60, 160, second)
turtle.hideturtle()
# 启动turtle,设置画布大小
def main():
turtle.setup(500, 500)
turtle.bgcolor('white')
turtle.title('Pointer Clock with Python Turtle')
draw_clock(1)
turtle.done()
if __name__ == '__main__':
main()
```
阅读全文