Benjamin Peterson | 9cf41d0 | 2010-03-11 22:33:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame] | 2 | # -*- coding: cp1252 -*- |
| 3 | """ turtle-example-suite: |
| 4 | |
| 5 | tdemo_clock.py |
| 6 | |
| 7 | Enhanced clock-program, showing date |
| 8 | and time |
| 9 | ------------------------------------ |
| 10 | Press STOP to exit the program! |
| 11 | ------------------------------------ |
| 12 | """ |
| 13 | from turtle import * |
| 14 | from datetime import datetime |
| 15 | |
| 16 | mode("logo") |
| 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 | |
| 46 | def clockface(radius): |
| 47 | reset() |
| 48 | pensize(7) |
| 49 | for i in range(60): |
| 50 | jump(radius) |
| 51 | if i % 5 == 0: |
| 52 | fd(25) |
| 53 | jump(-radius-25) |
| 54 | else: |
| 55 | dot(3) |
| 56 | jump(-radius) |
| 57 | rt(6) |
| 58 | |
| 59 | def setup(): |
| 60 | global second_hand, minute_hand, hour_hand, writer |
| 61 | mode("logo") |
| 62 | make_hand_shape("second_hand", 125, 25) |
| 63 | make_hand_shape("minute_hand", 130, 25) |
| 64 | make_hand_shape("hour_hand", 90, 25) |
| 65 | clockface(160) |
| 66 | second_hand = Turtle() |
| 67 | second_hand.shape("second_hand") |
| 68 | second_hand.color("gray20", "gray80") |
| 69 | minute_hand = Turtle() |
| 70 | minute_hand.shape("minute_hand") |
| 71 | minute_hand.color("blue1", "red1") |
| 72 | hour_hand = Turtle() |
| 73 | hour_hand.shape("hour_hand") |
| 74 | hour_hand.color("blue3", "red3") |
| 75 | for hand in second_hand, minute_hand, hour_hand: |
| 76 | hand.resizemode("user") |
| 77 | hand.shapesize(1, 1, 3) |
| 78 | hand.speed(0) |
| 79 | ht() |
| 80 | writer = Turtle() |
| 81 | #writer.mode("logo") |
| 82 | writer.ht() |
| 83 | writer.pu() |
| 84 | writer.bk(85) |
| 85 | |
| 86 | |
| 87 | def wochentag(t): |
| 88 | wochentag = ["Monday", "Tuesday", "Wednesday", |
| 89 | "Thursday", "Friday", "Saturday", "Sunday"] |
| 90 | return wochentag[t.weekday()] |
| 91 | |
| 92 | def datum(z): |
| 93 | monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June", |
| 94 | "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."] |
| 95 | j = z.year |
| 96 | m = monat[z.month - 1] |
| 97 | t = z.day |
| 98 | return "%s %d %d" % (m, t, j) |
| 99 | |
| 100 | def tick(): |
| 101 | t = datetime.today() |
| 102 | sekunde = t.second + t.microsecond*0.000001 |
| 103 | minute = t.minute + sekunde/60.0 |
| 104 | stunde = t.hour + minute/60.0 |
| 105 | tracer(False) |
| 106 | writer.clear() |
| 107 | writer.home() |
| 108 | writer.forward(65) |
| 109 | writer.write(wochentag(t), |
| 110 | align="center", font=("Courier", 14, "bold")) |
| 111 | writer.back(150) |
| 112 | writer.write(datum(t), |
| 113 | align="center", font=("Courier", 14, "bold")) |
| 114 | writer.forward(85) |
| 115 | tracer(True) |
| 116 | second_hand.setheading(6*sekunde) |
| 117 | minute_hand.setheading(6*minute) |
| 118 | hour_hand.setheading(30*stunde) |
| 119 | tracer(True) |
| 120 | ontimer(tick, 100) |
| 121 | |
| 122 | def main(): |
| 123 | tracer(False) |
| 124 | setup() |
| 125 | tracer(True) |
| 126 | tick() |
| 127 | return "EVENTLOOP" |
| 128 | |
| 129 | if __name__ == "__main__": |
| 130 | msg = main() |
| 131 | print msg |
| 132 | mainloop() |