|
1 |
| -# -*- coding: cp1252 -*- |
2 | 1 | """ turtle-example-suite:
|
3 | 2 |
|
4 | 3 | tdemo_clock.py
|
@@ -52,62 +51,54 @@ def clockface(radius):
|
52 | 51 | jump(-radius)
|
53 | 52 | rt(6)
|
54 | 53 |
|
| 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 | + |
55 | 75 | def setup():
|
56 | 76 | global second_hand, minute_hand, hour_hand, writer
|
57 | 77 | mode("logo")
|
58 | 78 | make_hand_shape("second_hand", 125, 25)
|
59 |
| - make_hand_shape("minute_hand", 130, 25) |
| 79 | + make_hand_shape("minute_hand", 115, 25) |
60 | 80 | make_hand_shape("hour_hand", 90, 25)
|
61 | 81 | 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 | + |
75 | 87 | ht()
|
76 | 88 | writer = Turtle()
|
77 |
| - #writer.mode("logo") |
78 | 89 | writer.ht()
|
79 | 90 | writer.pu()
|
80 | 91 | 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() |
94 | 93 |
|
95 | 94 | 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 | + |
100 | 100 | try:
|
101 | 101 | 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) |
111 | 102 | second_hand.setheading(6*sekunde) # or here
|
112 | 103 | minute_hand.setheading(6*minute)
|
113 | 104 | hour_hand.setheading(30*stunde)
|
|
0 commit comments