/Demo/turtle/tdemo_clock.py

http://unladen-swallow.googlecode.com/ · Python · 132 lines · 106 code · 13 blank · 13 comment · 5 complexity · b9784e7cd91d146ffa4c6eb7ef25e771 MD5 · raw file

  1. #!/usr/bin/python
  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. mode("logo")
  14. def jump(distanz, winkel=0):
  15. penup()
  16. right(winkel)
  17. forward(distanz)
  18. left(winkel)
  19. pendown()
  20. def hand(laenge, spitze):
  21. fd(laenge*1.15)
  22. rt(90)
  23. fd(spitze/2.0)
  24. lt(120)
  25. fd(spitze)
  26. lt(120)
  27. fd(spitze)
  28. lt(120)
  29. fd(spitze/2.0)
  30. def make_hand_shape(name, laenge, spitze):
  31. reset()
  32. jump(-laenge*0.15)
  33. begin_poly()
  34. hand(laenge, spitze)
  35. end_poly()
  36. hand_form = get_poly()
  37. register_shape(name, hand_form)
  38. def clockface(radius):
  39. reset()
  40. pensize(7)
  41. for i in range(60):
  42. jump(radius)
  43. if i % 5 == 0:
  44. fd(25)
  45. jump(-radius-25)
  46. else:
  47. dot(3)
  48. jump(-radius)
  49. rt(6)
  50. def setup():
  51. global second_hand, minute_hand, hour_hand, writer
  52. mode("logo")
  53. make_hand_shape("second_hand", 125, 25)
  54. make_hand_shape("minute_hand", 130, 25)
  55. make_hand_shape("hour_hand", 90, 25)
  56. clockface(160)
  57. second_hand = Turtle()
  58. second_hand.shape("second_hand")
  59. second_hand.color("gray20", "gray80")
  60. minute_hand = Turtle()
  61. minute_hand.shape("minute_hand")
  62. minute_hand.color("blue1", "red1")
  63. hour_hand = Turtle()
  64. hour_hand.shape("hour_hand")
  65. hour_hand.color("blue3", "red3")
  66. for hand in second_hand, minute_hand, hour_hand:
  67. hand.resizemode("user")
  68. hand.shapesize(1, 1, 3)
  69. hand.speed(0)
  70. ht()
  71. writer = Turtle()
  72. #writer.mode("logo")
  73. writer.ht()
  74. writer.pu()
  75. writer.bk(85)
  76. def wochentag(t):
  77. wochentag = ["Monday", "Tuesday", "Wednesday",
  78. "Thursday", "Friday", "Saturday", "Sunday"]
  79. return wochentag[t.weekday()]
  80. def datum(z):
  81. monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
  82. "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
  83. j = z.year
  84. m = monat[z.month - 1]
  85. t = z.day
  86. return "%s %d %d" % (m, t, j)
  87. def tick():
  88. t = datetime.today()
  89. sekunde = t.second + t.microsecond*0.000001
  90. minute = t.minute + sekunde/60.0
  91. stunde = t.hour + minute/60.0
  92. tracer(False)
  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. tracer(True)
  103. second_hand.setheading(6*sekunde)
  104. minute_hand.setheading(6*minute)
  105. hour_hand.setheading(30*stunde)
  106. tracer(True)
  107. ontimer(tick, 100)
  108. def main():
  109. tracer(False)
  110. setup()
  111. tracer(True)
  112. tick()
  113. return "EVENTLOOP"
  114. if __name__ == "__main__":
  115. msg = main()
  116. print msg
  117. mainloop()