使用turtle 绘制数字时钟和模拟时钟。
模拟时钟代码
# -*- coding:utf-8 –*-
# 用turtlr画时钟
# 以自定义shape的方式实现
import turtle as t
import datetime as d
def skip(step): # 抬笔,跳到一个地方
t.penup()
t.forward(step)
t.pendown()
def drawClock(radius): # 画表盘
t.speed(0)
t.mode("logo") # 以Logo坐标、角度方式
t.hideturtle()
t.pensize(7)
t.home() # 回到圆点
for j in range(60):
skip(radius)
if (j % 5 == 0):
t.forward(20)
skip(-radius - 20)
else:
t.dot(5)
skip(-radius)
t.right(6)
def makePoint(pointName, len): # 钟的指针,时针、分针、秒针
t.penup()
t.home()
t.begin_poly()
t.back(0.1 * len)
t.forward(len * 1.1)
t.end_poly()
poly = t.get_poly()
t.register_shape(pointName, poly) # 注册为一个shape
def drawPoint(): # 画指针
global hourPoint, minPoint, secPoint, fontWriter
makePoint("hourPoint", 100)
makePoint("minPoint", 120)
makePoint("secPoint", 140)
hourPoint = t.Pen() # 每个指针是一只新turtle
hourPoint.shape("hourPoint")
hourPoint.shapesize