clock.py 3.1 KB

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