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