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