annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/clock.py @ 69:33d812a61356

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