annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/clock.py @ 68:5028fdace37b

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