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