Today I discovered the turtle library for python.
If you are old enough to remember the educational programming language Logo, then you might remember spending hours in your school’s BBC microcomputer lab playing with this electronic turtle.
Logo provides a way of drawing line graphics and patterns using simple commands such as ‘forward’ and ‘right’. Logo is a good way of teaching and learning ‘Computational Thinking’ or the basics of programming. Today you can experience the same fun on your Raspberry Pi or Python interpreter on your PC, as the turtle library is included in the standard distribution.
After playing with a few random walks (which I may post at a later date), I had a go at creating a working analogue clock, and I am rather pleased with the results. Thanks to Sonny for his enthusiasm and suggested improvements.
Python 3 analogue clock using the turtle graphics library.
You can download the code, or copy it from below.
#Turtle Analogue Clock
#SHOWS GMT NOT BST
print("Python Turtle Analogue Clock")
print("By T Street")
#Deal with different time zones
ok = False
print("\nFor example, for British summer time enter 1")
offset = int(input("Enter offset from GMT (-11 to 11) :"))
if offset >= -11 and offset <= 11:
ok = True
wn = turtle.Screen()
SCALE = 1.7 # size of clock scale factor (try 2.0 to 0.5)
mark = turtle.Turtle()
for i in range(60):
if i % 5 == 0:
update = True #controls whether minute and hour hand should update (once per minute)
updateSecond = True # controls whether the second hanbd should update
b = time.gmtime(time.time()) # current GMT
m = b.tm_min # remember the current minute
s = b.tm_sec # rember the current second
hour = turtle.Turtle()
hour.right(((b.tm_hour + offset) % 12) * 30 + b.tm_min * 0.5 )
minute = turtle.Turtle()
minute.right((b.tm_min) * 6)
update = False
second = turtle.Turtle()
second.right((b.tm_sec) * 6)
updateSecond = False
b = time.gmtime(time.time())
new_min = b.tm_min
new_sec = b.tm_sec
if new_min != m:
update = True
hour.clear() # Clear out the drawing (if any)
if new_sec != s:
updateSecond = True
Originally published on Blogger