summaryrefslogtreecommitdiffstats
path: root/Demo/turtle/tdemo_clock.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-06-10 04:44:07 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2008-06-10 04:44:07 (GMT)
commit97cf99fc7e8c56868711f7c91c3b1e37e4c66cba (patch)
tree6b43643ebc4dc74dd084df06e9163134818423bf /Demo/turtle/tdemo_clock.py
parentb38fea34bfc67ab5780804a511efe34f49d11e45 (diff)
downloadcpython-97cf99fc7e8c56868711f7c91c3b1e37e4c66cba.zip
cpython-97cf99fc7e8c56868711f7c91c3b1e37e4c66cba.tar.gz
cpython-97cf99fc7e8c56868711f7c91c3b1e37e4c66cba.tar.bz2
Patch #3064: Port new turtle module and demos to 3.0.
Diffstat (limited to 'Demo/turtle/tdemo_clock.py')
-rw-r--r--Demo/turtle/tdemo_clock.py132
1 files changed, 132 insertions, 0 deletions
diff --git a/Demo/turtle/tdemo_clock.py b/Demo/turtle/tdemo_clock.py
new file mode 100644
index 0000000..c91d4c5
--- /dev/null
+++ b/Demo/turtle/tdemo_clock.py
@@ -0,0 +1,132 @@
+#!/usr/bin/python
+# -*- coding: cp1252 -*-
+""" turtle-example-suite:
+
+ tdemo_clock.py
+
+Enhanced clock-program, showing date
+and time
+ ------------------------------------
+ Press STOP to exit the program!
+ ------------------------------------
+"""
+from tkinter.turtle import *
+from datetime import datetime
+
+mode("logo")
+
+def jump(distanz, winkel=0):
+ penup()
+ right(winkel)
+ forward(distanz)
+ left(winkel)
+ pendown()
+
+def hand(laenge, spitze):
+ fd(laenge*1.15)
+ rt(90)
+ fd(spitze/2.0)
+ lt(120)
+ fd(spitze)
+ lt(120)
+ fd(spitze)
+ lt(120)
+ fd(spitze/2.0)
+
+def make_hand_shape(name, laenge, spitze):
+ reset()
+ jump(-laenge*0.15)
+ begin_poly()
+ hand(laenge, spitze)
+ end_poly()
+ hand_form = get_poly()
+ register_shape(name, hand_form)
+
+
+def clockface(radius):
+ reset()
+ pensize(7)
+ for i in range(60):
+ jump(radius)
+ if i % 5 == 0:
+ fd(25)
+ jump(-radius-25)
+ else:
+ dot(3)
+ jump(-radius)
+ rt(6)
+
+def setup():
+ global second_hand, minute_hand, hour_hand, writer
+ mode("logo")
+ make_hand_shape("second_hand", 125, 25)
+ make_hand_shape("minute_hand", 130, 25)
+ make_hand_shape("hour_hand", 90, 25)
+ clockface(160)
+ second_hand = Turtle()
+ second_hand.shape("second_hand")
+ second_hand.color("gray20", "gray80")
+ minute_hand = Turtle()
+ minute_hand.shape("minute_hand")
+ minute_hand.color("blue1", "red1")
+ hour_hand = Turtle()
+ hour_hand.shape("hour_hand")
+ hour_hand.color("blue3", "red3")
+ for hand in second_hand, minute_hand, hour_hand:
+ hand.resizemode("user")
+ hand.shapesize(1, 1, 3)
+ hand.speed(0)
+ ht()
+ writer = Turtle()
+ #writer.mode("logo")
+ writer.ht()
+ writer.pu()
+ writer.bk(85)
+
+
+def wochentag(t):
+ wochentag = ["Monday", "Tuesday", "Wednesday",
+ "Thursday", "Friday", "Saturday", "Sunday"]
+ return wochentag[t.weekday()]
+
+def datum(z):
+ monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
+ "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
+ j = z.year
+ m = monat[z.month - 1]
+ t = z.day
+ return "%s %d %d" % (m, t, j)
+
+def tick():
+ t = datetime.today()
+ sekunde = t.second + t.microsecond*0.000001
+ minute = t.minute + sekunde/60.0
+ stunde = t.hour + minute/60.0
+ tracer(False)
+ writer.clear()
+ writer.home()
+ writer.forward(65)
+ writer.write(wochentag(t),
+ align="center", font=("Courier", 14, "bold"))
+ writer.back(150)
+ writer.write(datum(t),
+ align="center", font=("Courier", 14, "bold"))
+ writer.forward(85)
+ tracer(True)
+ second_hand.setheading(6*sekunde)
+ minute_hand.setheading(6*minute)
+ hour_hand.setheading(30*stunde)
+ tracer(True)
+ ontimer(tick, 100)
+
+def main():
+ tracer(False)
+ setup()
+ tracer(True)
+ tick()
+ return "EVENTLOOP"
+
+if __name__ == "__main__":
+ msg = main()
+ print(msg)
+ mainloop()