Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f4ccabd

Browse files
committed
提交代码
1 parent 60035bb commit f4ccabd

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

‎.DS_Store

0 Bytes
Binary file not shown.

‎xianhuan/.DS_Store

0 Bytes
Binary file not shown.

‎xianhuan/drawclock/drawclock.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import turtle
7+
from datetime import *
8+
9+
# 抬起画笔,向前运动一段距离放下
10+
def skip(step):
11+
turtle.penup()
12+
turtle.forward(step)
13+
turtle.pendown()
14+
15+
def mkHand(name, length):
16+
# 注册Turtle形状,建立表针Turtle
17+
turtle.reset()
18+
# 先反向运动一段距离,终点作为绘制指针的起点
19+
skip(-length * 0.1)
20+
# 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
21+
turtle.begin_poly()
22+
turtle.forward(length * 1.1)
23+
# 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
24+
turtle.end_poly()
25+
# 返回最后记录的多边形。
26+
handForm = turtle.get_poly()
27+
turtle.register_shape(name, handForm)
28+
29+
def init():
30+
global secHand, minHand, houHand, printer
31+
# 重置Turtle指向北
32+
turtle.mode("logo")
33+
# 建立三个表针Turtle并初始化
34+
mkHand("secHand", 135)
35+
mkHand("minHand", 125)
36+
mkHand("houHand", 90)
37+
secHand = turtle.Turtle()
38+
secHand.shape("secHand")
39+
minHand = turtle.Turtle()
40+
minHand.shape("minHand")
41+
houHand = turtle.Turtle()
42+
houHand.shape("houHand")
43+
44+
for hand in secHand, minHand, houHand:
45+
hand.shapesize(1, 1, 3)
46+
hand.speed(0)
47+
48+
# 建立输出文字Turtle
49+
printer = turtle.Turtle()
50+
# 隐藏画笔的turtle形状
51+
printer.hideturtle()
52+
printer.penup()
53+
54+
# 绘制表盘
55+
def setupClock(radius):
56+
# 建立表的外框
57+
turtle.reset()
58+
turtle.pensize(7)
59+
for i in range(60):
60+
# 向前移动半径值
61+
skip(radius)
62+
if i % 5 == 0:
63+
# 画长刻度线
64+
turtle.forward(20)
65+
# 回到中心点
66+
skip(-radius - 20)
67+
68+
# 移动到刻度线终点
69+
skip(radius + 20)
70+
# 下面是写表盘刻度值,为了不与划线重叠,所以对于特殊位置做了处理
71+
if i == 0:
72+
turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
73+
elif i == 30:
74+
skip(25)
75+
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
76+
skip(-25)
77+
elif (i == 25 or i == 35):
78+
skip(20)
79+
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
80+
skip(-20)
81+
else:
82+
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
83+
84+
# 回到中心点
85+
skip(-radius - 20)
86+
else:
87+
# 画圆点
88+
turtle.dot(5)
89+
skip(-radius)
90+
# 顺时针移动6°
91+
turtle.right(6)
92+
93+
def week(t):
94+
week = ["星期一", "星期二", "星期三",
95+
"星期四", "星期五", "星期六", "星期日"]
96+
return week[t.weekday()]
97+
98+
def date(t):
99+
y = t.year
100+
m = t.month
101+
d = t.day
102+
return "%s %d%d" % (y, m, d)
103+
104+
def tick():
105+
# 绘制表针的动态显示
106+
t = datetime.today()
107+
second = t.second + t.microsecond * 0.000001
108+
minute = t.minute + second / 60.0
109+
hour = t.hour + minute / 60.0
110+
secHand.setheading(6 * second)
111+
minHand.setheading(6 * minute)
112+
houHand.setheading(30 * hour)
113+
114+
turtle.tracer(False)
115+
printer.forward(65)
116+
printer.write(week(t), align="center",
117+
font=("Courier", 14, "bold"))
118+
printer.back(130)
119+
printer.write(date(t), align="center",
120+
font=("Courier", 14, "bold"))
121+
printer.home()
122+
turtle.tracer(True)
123+
124+
# 100ms后继续调用tick
125+
turtle.ontimer(tick, 100)
126+
127+
def main():
128+
# 打开/关闭龟动画,并为更新图纸设置延迟。
129+
turtle.tracer(False)
130+
init()
131+
setupClock(160)
132+
turtle.tracer(True)
133+
tick()
134+
turtle.mainloop()
135+
136+
if __name__ == "__main__":
137+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /