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