0% found this document useful (0 votes)
3 views3 pages

Clock

This Python script creates an enhanced clock using the Turtle graphics library, displaying the current date and time. It defines functions to draw the clock face and hands, as well as to update the time every second. The program runs until the user stops it, providing an interactive visual representation of time.

Uploaded by

inshaalispro69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Clock

This Python script creates an enhanced clock using the Turtle graphics library, displaying the current date and time. It defines functions to draw the clock face and hands, as well as to update the time every second. The program runs until the user stops it, providing an interactive visual representation of time.

Uploaded by

inshaalispro69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#!

/usr/bin/env python3
# -*- coding: cp1252 -*-
""" turtle-example-suite:

tdemo_clock.py

Enhanced clock-program, showing date


and time
------------------------------------
Press STOP to exit the program!
------------------------------------
"""
from turtle import *
from datetime import datetime

def jump(distanz, winkel=0):


penup()
right(winkel)
forward(distanz)
left(winkel)
pendown()

def hand(laenge, spitze):


fd(laenge*1.15)
rt(90)
fd(spitze/2.0)
lt(120)
fd(spitze)
lt(120)
fd(spitze)
lt(120)
fd(spitze/2.0)

def make_hand_shape(name, laenge, spitze):


reset()
jump(-laenge*0.15)
begin_poly()
hand(laenge, spitze)
end_poly()
hand_form = get_poly()
register_shape(name, hand_form)

def clockface(radius):
reset()
pensize(7)
for i in range(60):
jump(radius)
if i % 5 == 0:
fd(25)
jump(-radius-25)
else:
dot(3)
jump(-radius)
rt(6)

def setup():
global second_hand, minute_hand, hour_hand, writer
mode("logo")
make_hand_shape("second_hand", 125, 25)
make_hand_shape("minute_hand", 130, 25)
make_hand_shape("hour_hand", 90, 25)
clockface(160)
second_hand = Turtle()
second_hand.shape("second_hand")
second_hand.color("gray20", "gray80")
minute_hand = Turtle()
minute_hand.shape("minute_hand")
minute_hand.color("blue1", "red1")
hour_hand = Turtle()
hour_hand.shape("hour_hand")
hour_hand.color("blue3", "red3")
for hand in second_hand, minute_hand, hour_hand:
[Link]("user")
[Link](1, 1, 3)
[Link](0)
ht()
writer = Turtle()
#[Link]("logo")
[Link]()
[Link]()
[Link](85)

def wochentag(t):
wochentag = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
return wochentag[[Link]()]

def datum(z):
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
j = [Link]
m = monat[[Link] - 1]
t = [Link]
return "%s %d %d" % (m, t, j)

def tick():
t = [Link]()
sekunde = [Link] + [Link]*0.000001
minute = [Link] + sekunde/60.0
stunde = [Link] + minute/60.0
try:
tracer(False) # Terminator can occur here
[Link]()
[Link]()
[Link](65)
[Link](wochentag(t),
align="center", font=("Courier", 14, "bold"))
[Link](150)
[Link](datum(t),
align="center", font=("Courier", 14, "bold"))
[Link](85)
second_hand.setheading(6*sekunde) # or here
minute_hand.setheading(6*minute)
hour_hand.setheading(30*stunde)
tracer(True)
ontimer(tick, 100)
except Terminator:
pass # turtledemo user pressed STOP
def main():
tracer(False)
setup()
tracer(True)
tick()
return "EVENTLOOP"

if __name__ == "__main__":
mode("logo")
msg = main()
print("The clock uses the system date and time, so it is quite accurate")
mainloop()

You might also like