赞
踩
- # coding=utf-8
-
- import turtle
- from datetime import *
-
- # 抬起画笔,向前运动一段距离放下
- def Skip(step):
- turtle.penup() # penup() | pu() | up():将提起笔,移动时无图
- turtle.forward(step) # forward() 向前移动指定的距离
- turtle.pendown() # pendown() | pd() | down():放下笔,移动的时候绘图
-
- def mkHand(name, length):
- # 注册Turtle形状,建立表针Turtle
- turtle.reset() # reset():重置,将屏幕中的图纸删除,重新居中并将所有变量设置为默认值
- Skip(-length * 0.1)
- # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
- turtle.begin_poly() # begin_poly(): 开始记录多边形的顶点
- turtle.forward(length * 1.1)
- # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
- turtle.end_poly() # end_poly(): 停止记录多边形的顶点
- # 返回最后记录的多边形。
- handForm = turtle.get_poly() # get_poly():返回最后记录的多边形
- turtle.register_shape(name, handForm) # 内存中添加注册海龟图形 #将“name” Shape添加到TurtleScreen对象的shapelist中
-
- def Init():
- global secHand, minHand, hurHand, printer
- # 重置Turtle指向北
- turtle.mode("logo")
- # 建立三个表针Turtle并初始化
- mkHand("secHand", 135)
- mkHand("minHand", 125)
- mkHand("hurHand", 90)
-
- secHand = turtle.Turtle()
- secHand.shape("secHand") # turtle.shape("myFavouriteShape") 使用register_shape中添加的shape turtle.shape(name=None) 设置或返回(海龟)画笔箭头形状名
-
- minHand = turtle.Turtle()
- minHand.shape("minHand")
-
- hurHand = turtle.Turtle()
- hurHand.shape("hurHand")
-
- for hand in secHand, minHand, hurHand:
- hand.shapesize(1, 1, 3) #shapesize 设置/返回笔的属性 turtle.shapesize(stretch_wid=None, stretch_len=None, outline=None) ;
- #stretch_wid – positive number 与当前箭头方向的垂直方向上的拉伸值
- #stretch_len – positive number 与当前箭头方向上的拉伸值
- #outline – positive number shape轮廓宽度
- hand.speed(0)
-
- # 建立输出文字Turtle
- printer = turtle.Turtle()
- # 隐藏画笔的turtle形状
- printer.hideturtle() #hideturtle() | ht() :删隐藏乌龟的形状,在做复杂绘图的时候隐藏有利于提高绘图速度
- printer.penup()
-
-
- def SetupClock(radius):
- # 建立表的外框
- turtle.reset()
- turtle.pensize(7) #设置或返回线的粗细
- for i in range(60):
- Skip(radius)
- if i % 5 == 0:
- turtle.forward(20)
- Skip(-radius - 20)
-
- Skip(radius + 20)
- if i == 0:
- turtle.write(int(12), align="center", font=("Courier", 14, "bold")) #write():写文本。参数:arg-要写入的对象。move-是否移动。align-对齐方式:left,right,center。font-字体。fontname,fontsize,fonttype
- elif i == 30:
- Skip(25)
- turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
- Skip(-25)
- elif (i == 25 or i == 35):
- Skip(20)
- turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
- Skip(-20)
- else:
- turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
- Skip(-radius - 20)
- else:
- turtle.dot(5) # 用颜色画出一个直径大小的圆点
- Skip(-radius)
- turtle.right(6) # right() | rt():以角度单位向右转动
-
- def Week(t):
- week = ["Monday", "Tuesday", "Wednesday","Thursday","Friday", "Saturday", "Sunday"]
- return week[t.weekday()]
-
- def Date(t):
- y = t.year
- m = t.month
- d = t.day
- return "%s %d%d" % (y, m, d)
-
- def Tick():
- # 绘制表针的动态显示
- t = datetime.today()
- second = t.second + t.microsecond * 0.000001
- minute = t.minute + second / 60.0
- hour = t.hour + minute / 60.0
- secHand.setheading(6 * second) #setheading()|seth(): 将方向设置为to_angle.就是东西南北方向。具体如下:标准模式:0 - 东 90 - 北 180 - 西 270 - 南 标志模式 0- 北 90- 东 180- 南 270 - 西
- minHand.setheading(6 * minute)
- hurHand.setheading(30 * hour)
-
- turtle.tracer(False)
- printer.forward(65)
- printer.write(Week(t), align="center",
- font=("Courier", 14, "bold"))
- printer.back(130)
- printer.write(Date(t), align="center",
- font=("Courier", 14, "bold"))
- printer.home() # home() : 移动到原点 - 坐标(0,0):并将其标题设置为其起始方向(取决于模式)
- turtle.tracer(True)
-
- # 100ms后继续调用tick
- turtle.ontimer(Tick, 100)
-
- def main():
- # 打开/关闭龟动画,并为更新图纸设置延迟。
- turtle.tracer(False)
- Init()
- SetupClock(160)
- turtle.tracer(True)
- Tick()
- turtle.mainloop() # mainloop() | done() :开始循环
-
- if __name__ == "__main__":
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。