jpayne@68: #!/usr/bin/env python3 jpayne@68: # -*- coding: cp1252 -*- jpayne@68: """ turtle-example-suite: jpayne@68: jpayne@68: tdemo_clock.py jpayne@68: jpayne@68: Enhanced clock-program, showing date jpayne@68: and time jpayne@68: ------------------------------------ jpayne@68: Press STOP to exit the program! jpayne@68: ------------------------------------ jpayne@68: """ jpayne@68: from turtle import * jpayne@68: from datetime import datetime jpayne@68: jpayne@68: def jump(distanz, winkel=0): jpayne@68: penup() jpayne@68: right(winkel) jpayne@68: forward(distanz) jpayne@68: left(winkel) jpayne@68: pendown() jpayne@68: jpayne@68: def hand(laenge, spitze): jpayne@68: fd(laenge*1.15) jpayne@68: rt(90) jpayne@68: fd(spitze/2.0) jpayne@68: lt(120) jpayne@68: fd(spitze) jpayne@68: lt(120) jpayne@68: fd(spitze) jpayne@68: lt(120) jpayne@68: fd(spitze/2.0) jpayne@68: jpayne@68: def make_hand_shape(name, laenge, spitze): jpayne@68: reset() jpayne@68: jump(-laenge*0.15) jpayne@68: begin_poly() jpayne@68: hand(laenge, spitze) jpayne@68: end_poly() jpayne@68: hand_form = get_poly() jpayne@68: register_shape(name, hand_form) jpayne@68: jpayne@68: def clockface(radius): jpayne@68: reset() jpayne@68: pensize(7) jpayne@68: for i in range(60): jpayne@68: jump(radius) jpayne@68: if i % 5 == 0: jpayne@68: fd(25) jpayne@68: jump(-radius-25) jpayne@68: else: jpayne@68: dot(3) jpayne@68: jump(-radius) jpayne@68: rt(6) jpayne@68: jpayne@68: def setup(): jpayne@68: global second_hand, minute_hand, hour_hand, writer jpayne@68: mode("logo") jpayne@68: make_hand_shape("second_hand", 125, 25) jpayne@68: make_hand_shape("minute_hand", 130, 25) jpayne@68: make_hand_shape("hour_hand", 90, 25) jpayne@68: clockface(160) jpayne@68: second_hand = Turtle() jpayne@68: second_hand.shape("second_hand") jpayne@68: second_hand.color("gray20", "gray80") jpayne@68: minute_hand = Turtle() jpayne@68: minute_hand.shape("minute_hand") jpayne@68: minute_hand.color("blue1", "red1") jpayne@68: hour_hand = Turtle() jpayne@68: hour_hand.shape("hour_hand") jpayne@68: hour_hand.color("blue3", "red3") jpayne@68: for hand in second_hand, minute_hand, hour_hand: jpayne@68: hand.resizemode("user") jpayne@68: hand.shapesize(1, 1, 3) jpayne@68: hand.speed(0) jpayne@68: ht() jpayne@68: writer = Turtle() jpayne@68: #writer.mode("logo") jpayne@68: writer.ht() jpayne@68: writer.pu() jpayne@68: writer.bk(85) jpayne@68: jpayne@68: def wochentag(t): jpayne@68: wochentag = ["Monday", "Tuesday", "Wednesday", jpayne@68: "Thursday", "Friday", "Saturday", "Sunday"] jpayne@68: return wochentag[t.weekday()] jpayne@68: jpayne@68: def datum(z): jpayne@68: monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June", jpayne@68: "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."] jpayne@68: j = z.year jpayne@68: m = monat[z.month - 1] jpayne@68: t = z.day jpayne@68: return "%s %d %d" % (m, t, j) jpayne@68: jpayne@68: def tick(): jpayne@68: t = datetime.today() jpayne@68: sekunde = t.second + t.microsecond*0.000001 jpayne@68: minute = t.minute + sekunde/60.0 jpayne@68: stunde = t.hour + minute/60.0 jpayne@68: try: jpayne@68: tracer(False) # Terminator can occur here jpayne@68: writer.clear() jpayne@68: writer.home() jpayne@68: writer.forward(65) jpayne@68: writer.write(wochentag(t), jpayne@68: align="center", font=("Courier", 14, "bold")) jpayne@68: writer.back(150) jpayne@68: writer.write(datum(t), jpayne@68: align="center", font=("Courier", 14, "bold")) jpayne@68: writer.forward(85) jpayne@68: tracer(True) jpayne@68: second_hand.setheading(6*sekunde) # or here jpayne@68: minute_hand.setheading(6*minute) jpayne@68: hour_hand.setheading(30*stunde) jpayne@68: tracer(True) jpayne@68: ontimer(tick, 100) jpayne@68: except Terminator: jpayne@68: pass # turtledemo user pressed STOP jpayne@68: jpayne@68: def main(): jpayne@68: tracer(False) jpayne@68: setup() jpayne@68: tracer(True) jpayne@68: tick() jpayne@68: return "EVENTLOOP" jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: mode("logo") jpayne@68: msg = main() jpayne@68: print(msg) jpayne@68: mainloop()