Debian Codegraph @

Lib/turtledemo/clock.py

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