analog clock


# import winsound
from turtle import Turtle, Screen, Terminator
from datetime import datetime

screen = Screen()
screen.mode('logo')
screen.setup(700, 700)
screen.title('Alarm Clock')

tag_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
monat = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June', 'July', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.']

FONT_SIZE = 14
FONT = ('Courier', FONT_SIZE, 'bold')
BUTTON_SIZE = 40
CURSOR_SIZE = 20

def jump(turtle, distance):
    turtle.penup()
    turtle.forward(distance)
    turtle.pendown()

def hand(turtle, laenge, spitze):
    turtle.fd(laenge * 1.10)
    turtle.rt(90)
    turtle.fd(spitze / 20.0)
    turtle.lt(120)
    turtle.fd(spitze)
    turtle.lt(120)
    turtle.fd(spitze)
    turtle.lt(120)
    turtle.fd(spitze / 20.0)
   
def make_hand_shape(turtle, name, laenge, spitze):
    turtle.reset()
    jump(turtle, -laenge * 0.15)
    turtle.begin_poly()
    hand(turtle, laenge, spitze)
    turtle.end_poly()
    screen.register_shape(name, turtle.get_poly())

def clockface(radius):
    face = Turtle()
    face.pensize(3)
    face.speed('fastest')

    jump(face, radius)
    face.left(90)
    face.circle(radius)
    face.right(90)
    jump(face, -radius)
    face.pensize(7)

    radius *= 0.8

    jump(face, radius)

    for i in range(60):
        if i % 5 == 0:
            face.forward(25)
            jump(face, -25)
        else:
            face.dot(4)

        face.left(90)
        face.penup()
        face.circle(radius, 6)
        face.pendown()
        face.right(90)

    face.hideturtle()

def setup():
    hand = Turtle()

    # clock hands
    make_hand_shape(hand, 'second_hand', 125, 5)
    make_hand_shape(hand, 'minute_hand', 130, 5)
    make_hand_shape(hand, 'hour_hand', 90, 5)

    hand.reset()
    hand.hideturtle()

    clockface(200)

    second_hand = Turtle()
    second_hand.shape('second_hand')
    second_hand.color('black')

    minute_hand = Turtle()
    minute_hand.shape('minute_hand')
    minute_hand.color('#505050')

    hour_hand = Turtle()
    hour_hand.shape('hour_hand')
    hour_hand.color('#191919')

    for hand in second_hand, minute_hand, hour_hand:
        hand.shapesize(outline=3)
        hand.speed('fastest')

    return second_hand, minute_hand, hour_hand

def tag(t):
    return tag_list[t.weekday()]
# date
def datum(z):
    j = z.year
    m = monat[z.month - 1]
    t = z.day

    return '%s %d %d' % (m, t, j)

def tick():

    t = datetime.today()

    sekunde = t.second + t.microsecond * 0.000001
    minute = t.minute + sekunde / 60.0
    stunde = t.hour + minute / 60.0

    d = datetime.now().strftime('%Y-%m-%d %H:%M')
    current_time = datetime.strptime(d, '%Y-%m-%d %H:%M').strftime('%Y-%m-%d %I:%M %p')

    try:
        marker1.undo()
        marker1.write(tag(t), align='center', font=FONT)

        marker2.undo()
        marker2.write(datum(t), align='center', font=FONT)

        marker3.undo()
        marker3.write(current_time, align='center', font=FONT)

        second_hand.setheading(6 * sekunde)
        minute_hand.setheading(6 * minute)
        hour_hand.setheading(30 * stunde)

        screen.update()

        if current_time in alarm_clock_dic:
            #winsound.PlaySound('SystemExit', winsound.SND_LOOP)
            #winsound.PlaySound('SystemExit', winsound.SND_LOOP)
            #winsound.PlaySound('SystemExit', winsound.SND_LOOP)
            #winsound.PlaySound('SystemExit', winsound.SND_LOOP)
            #winsound.PlaySound('SystemExit', winsound.SND_LOOP)
            pass

        screen.ontimer(tick, 100)
    except Terminator:
        screen.bye()

def set_alarm(x, y):
    d = datetime.now().strftime('%Y-%m-%d %H:%M')
    current_time = (datetime.strptime(d, '%Y-%m-%d %H:%M').strftime('%Y-%m-%d %I:%M %p'))

    pointSize = screen.textinput(current_time, "Enter Time")
    alarm_clock_dic.append(pointSize)

marker1 = Turtle(visible=False)
marker1.penup()
marker1.forward(65)
marker1.color('grey')
marker1.write('', align='center', font=FONT)

marker2 = Turtle(visible=False)
marker2.penup()
marker2.backward(85)
marker2.color('grey')
marker2.write('', align='center', font=FONT)

marker3 = Turtle(visible=False)
marker3.penup()
marker3.goto(0, 220)
marker3.color('grey')
marker3.write('', align='center', font=FONT)

screen.tracer(False)
second_hand, minute_hand, hour_hand = setup()

button = Turtle('circle')
button.shapesize(BUTTON_SIZE / CURSOR_SIZE, outline=2)
button.color('black', 'red')
button.penup()
button.goto(250, 250)  # move the button into position

marker4 = Turtle(visible=False)
marker4.penup()
marker4.goto(button.xcor(), button.ycor() - BUTTON_SIZE/2 - FONT_SIZE - 2)
marker4.write('Set Alarm', align='center', font=FONT)

screen.update()

alarm_clock_dic = []

button.onclick(set_alarm)

tick()

screen.mainloop()


Comments

Popular posts from this blog

program that reads two numbers and an arithmetic operator and displays the computed result.

program to find the multiples of a number ( the divisor) out of given five numbers.