Skip to content

Commit a0b922f

Browse files
committed
clock
1 parent c9930f5 commit a0b922f

File tree

2 files changed

+35
-42
lines changed

2 files changed

+35
-42
lines changed

Lib/turtledemo/clock.py

+33-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: cp1252 -*-
21
""" turtle-example-suite:
32
43
tdemo_clock.py
@@ -52,62 +51,54 @@ def clockface(radius):
5251
jump(-radius)
5352
rt(6)
5453

54+
def display_date_time():
55+
writer.clear()
56+
now = datetime.now()
57+
writer.home()
58+
writer.forward(distance=65)
59+
writer.write(now.strftime(format="%A"),
60+
align="center", font=("TkFixedFont", 14, "bold"))
61+
writer.back(distance=150)
62+
writer.write(now.strftime(format="%Y/%m/%d"),
63+
align="center", font=("TkFixedFont", 14, "bold"))
64+
writer.forward(distance=85)
65+
66+
def initialize_hand(shape, color):
67+
hand = Turtle()
68+
hand.shape(shape)
69+
hand.color(*color)
70+
hand.resizemode("user")
71+
hand.shapesize(1, 1, 3)
72+
hand.speed(0)
73+
return hand
74+
5575
def setup():
5676
global second_hand, minute_hand, hour_hand, writer
5777
mode("logo")
5878
make_hand_shape("second_hand", 125, 25)
59-
make_hand_shape("minute_hand", 130, 25)
79+
make_hand_shape("minute_hand", 115, 25)
6080
make_hand_shape("hour_hand", 90, 25)
6181
clockface(160)
62-
second_hand = Turtle()
63-
second_hand.shape("second_hand")
64-
second_hand.color("gray20", "gray80")
65-
minute_hand = Turtle()
66-
minute_hand.shape("minute_hand")
67-
minute_hand.color("blue1", "red1")
68-
hour_hand = Turtle()
69-
hour_hand.shape("hour_hand")
70-
hour_hand.color("blue3", "red3")
71-
for hand in second_hand, minute_hand, hour_hand:
72-
hand.resizemode("user")
73-
hand.shapesize(1, 1, 3)
74-
hand.speed(0)
82+
83+
second_hand = initialize_hand("second_hand", ("gray20", "gray80"))
84+
minute_hand = initialize_hand( "minute_hand", ("blue1", "red1"))
85+
hour_hand = initialize_hand( "hour_hand", ("blue3", "red3"))
86+
7587
ht()
7688
writer = Turtle()
77-
#writer.mode("logo")
7889
writer.ht()
7990
writer.pu()
8091
writer.bk(85)
81-
82-
def wochentag(t):
83-
wochentag = ["Monday", "Tuesday", "Wednesday",
84-
"Thursday", "Friday", "Saturday", "Sunday"]
85-
return wochentag[t.weekday()]
86-
87-
def datum(z):
88-
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
89-
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
90-
j = z.year
91-
m = monat[z.month - 1]
92-
t = z.day
93-
return "%s %d %d" % (m, t, j)
92+
display_date_time()
9493

9594
def tick():
96-
t = datetime.today()
97-
sekunde = t.second + t.microsecond*0.000001
98-
minute = t.minute + sekunde/60.0
99-
stunde = t.hour + minute/60.0
95+
now = datetime.now()
96+
sekunde = now.second + now.microsecond * 0.000001
97+
minute = now.minute + sekunde / 60.0
98+
stunde = now.hour + minute / 60.0
99+
100100
try:
101101
tracer(False) # Terminator can occur here
102-
writer.clear()
103-
writer.home()
104-
writer.forward(65)
105-
writer.write(wochentag(t),
106-
align="center", font=("Courier", 14, "bold"))
107-
writer.back(150)
108-
writer.write(datum(t),
109-
align="center", font=("Courier", 14, "bold"))
110-
writer.forward(85)
111102
second_hand.setheading(6*sekunde) # or here
112103
minute_hand.setheading(6*minute)
113104
hour_hand.setheading(30*stunde)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the canvas not clearing after running turtledemo clock, and improve the
2+
clock example suite

0 commit comments

Comments
 (0)