Debian Codegraph @

Lib/turtledemo/clock.py

raw
1 """turtledemo/clock.py
2
3 Enhanced clock-program, showing date
4 and time.
5 """
6 from turtle import *
7 from datetime import datetime
8
9 dtfont = "TkFixedFont", 14, "bold"
10 current_day = None
11
12 def jump(distanz, winkel=0):
13 penup()
14 right(winkel)
15 forward(distanz)
16 left(winkel)
17 pendown()
18
19 def hand(laenge, spitze):
20 fd(laenge*1.15)
21 rt(90)
22 fd(spitze/2.0)
23 lt(120)
24 fd(spitze)
25 lt(120)
26 fd(spitze)
27 lt(120)
28 fd(spitze/2.0)
29
30 def make_hand_shape(name, laenge, spitze):
31 reset()
32 jump(-laenge*0.15)
33 begin_poly()
34 hand(laenge, spitze)
35 end_poly()
36 hand_form = get_poly()
37 register_shape(name, hand_form)
38
39 def clockface(radius):
40 reset()
41 pensize(7)
42 for i in range(60):
43 jump(radius)
44 if i % 5 == 0:
45 fd(25)
46 jump(-radius-25)
47 else:
48 dot(3)
49 jump(-radius)
50 rt(6)
51
52 def display_date_time():
53 global current_day
54 writer.clear()
55 now = datetime.now()
56 current_day = now.day
57 writer.home()
58 writer.forward(distance=65)
59 writer.write(wochentag(now), align="center", font=dtfont)
60 writer.back(distance=150)
61 writer.write(datum(now), align="center", font=dtfont)
62 writer.forward(distance=85)
63
64 def setup():
65 global second_hand, minute_hand, hour_hand, writer
66 mode("logo")
67 make_hand_shape("second_hand", 125, 25)
68 make_hand_shape("minute_hand", 115, 25)
69 make_hand_shape("hour_hand", 90, 25)
70 clockface(160)
71 second_hand = Turtle()
72 second_hand.shape("second_hand")
73 second_hand.color("gray20", "gray80")
74 minute_hand = Turtle()
75 minute_hand.shape("minute_hand")
76 minute_hand.color("blue1", "red1")
77 hour_hand = Turtle()
78 hour_hand.shape("hour_hand")
79 hour_hand.color("blue3", "red3")
80 for hand in second_hand, minute_hand, hour_hand:
81 hand.resizemode("user")
82 hand.shapesize(1, 1, 3)
83 hand.speed(0)
84 ht()
85 writer = Turtle()
86 writer.ht()
87 writer.pu()
88 writer.bk(85)
89 display_date_time()
90
91 def wochentag(t):
92 wochentag = ["Monday", "Tuesday", "Wednesday",
93 "Thursday", "Friday", "Saturday", "Sunday"]
94 return wochentag[t.weekday()]
95
96 def datum(z):
97 monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
98 "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
99 j = z.year
100 m = monat[z.month - 1]
101 t = z.day
102 return "%s %d %d" % (m, t, j)
103
104 def tick():
105 t = datetime.today()
106 sekunde = t.second + t.microsecond*0.000001
107 minute = t.minute + sekunde/60.0
108 stunde = t.hour + minute/60.0
109 try:
110 tracer(False) # Terminator can occur here
111 second_hand.setheading(6*sekunde) # or here
112 minute_hand.setheading(6*minute)
113 hour_hand.setheading(30*stunde)
114 if t.day != current_day:
115 display_date_time()
116 tracer(True)
117 ontimer(tick, 100)
118 except Terminator:
119 pass # turtledemo user pressed STOP
120
121 def main():
122 tracer(False)
123 setup()
124 tracer(True)
125 tick()
126 return "EVENTLOOP"
127
128 if __name__ == "__main__":
129 mode("logo")
130 msg = main()
131 print(msg)
132 mainloop()