blob: 62c8851606b34e3f0189e6c7a71912b7f1f65b44 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Martin v. Löwis97cf99f2008-06-10 04:44:07 +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"""
Martin v. Löwis60ebb8b2008-09-21 07:32:10 +000013from turtle import *
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000014from datetime import datetime
15
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000016def jump(distanz, winkel=0):
17 penup()
18 right(winkel)
19 forward(distanz)
20 left(winkel)
21 pendown()
22
23def 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
34def 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öwis97cf99f2008-06-10 04:44:07 +000043def 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
56def 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öwis97cf99f2008-06-10 04:44:07 +000083def wochentag(t):
84 wochentag = ["Monday", "Tuesday", "Wednesday",
85 "Thursday", "Friday", "Saturday", "Sunday"]
86 return wochentag[t.weekday()]
87
88def 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
96def 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 Reedyf51531e2014-06-22 01:18:54 -0400101 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öwis97cf99f2008-06-10 04:44:07 +0000120
121def main():
122 tracer(False)
123 setup()
124 tracer(True)
125 tick()
126 return "EVENTLOOP"
127
128if __name__ == "__main__":
Terry Jan Reedyf5ac57d2014-06-30 16:09:24 -0400129 mode("logo")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000130 msg = main()
131 print(msg)
132 mainloop()