How to make an analog clock using Python Turtle Graphics – 3

Posted

in

by

Python Code

import turtle
from datetime import datetime

screen1 = turtle.Screen()
screen1.setup(500,500,0,0)
screen1.screensize(480,480, bg="#c0c0c0")
screen1.tracer(0)

don = turtle.Turtle()
#don.speed("fastest")
don.width(1)
don.hideturtle()

def draw_square(startx, starty, length):
    don.penup()
    don.home()
    don.goto(startx, starty)
    don.pendown()
    for side in range(4):
        don.forward(length)
        don.right(90)
    don.penup()

def draw_hand(length,rot):
    don.penup()
    don.home()
    don.pendown();
    don.right(1*rot+90*3)
    don.forward(length)
    don.penup()
    don.home()
    
def print_time(hou,minu,sec):
    don.penup()
    don.goto(-180,-190);
    don.pendown()
    data = str(hou)+" : "+str(minu)+" : "+str(sec)
    don.color("white")
    don.write(data, move = False,align='left', font=('Arial', 16, 'normal'));
    don.penup()

def draw_watchface():
    don.penup()
    for x in range(0,360,30):
        don.home()
        don.color("black")
        don.right(x)
        don.forward(150)
        don.pendown();
        don.forward(20)
        don.penup()
        
    for x in range(0,360,6):
        don.home()
        don.color("black")
        don.width(1)
        don.right(x)
        don.forward(150)
        don.pendown();
        don.forward(10)
        don.penup()
    don.penup()
    
def draw_frame(hou,minu,sec):
    don.goto(-200,200);
    don.pendown()
    draw_square(-200,200,400)
    draw_watchface()
    don.color("blue")
    draw_hand(140,sec*6)  #seconds
    don.color("red");
    don.width(3)
    draw_hand(140,minu*6)  #minutes
    don.color("green");
    don.width(10)
    draw_hand(140,hou*6*5)  #hours
    don.width(2)
    don.penup()
    don.goto(-220,-230);
    don.pendown()
    don.write("exasub.com", move = False,align='left', font=('Arial', 16, 'normal'));
    don.penup()
    print_time(hou,minu,sec)
    #print(str(hou)+" : "+str(minu)+" : "+str(sec))

def draw_time():
    while(1):
        t = datetime.today()
        sekunde = t.second 
        minuten = t.minute 
        houren = t.hour
        
        don.clear()
        draw_frame(houren,minuten,sekunde);
        screen1.update()

    
if __name__ == "__main__":
    draw_time()
    turtle.done()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *