turtle模拟时钟程序_函数

海龟模块制作的模拟时钟_python海龟画图模块制作_by_李兴球

"""这是一个用海龟模块制作的模拟时钟程序,作者:李兴球,日期:2018/12/10于杭州"""

from turtle import *
from time import *

def init_screen():
    
    screen = Screen()
    screen.title("模拟时钟")
    screen.delay(0)
    screen.mode("logo")                                       # 此模式刚好和时钟转动相适配
    pointer = ((0,0),(5,0),(5,50),(10,50),(0,60),(-10,50),(-5,50),(-5,0)) #顶点表
    screen.addshape("pointer",pointer)                        # 添加大箭头各顶点到形状列表
    
    return screen
    
def draw_digital():
    
    #以下代码画时钟的数字
    radius = 300
    draw_turtle = Turtle(visible=False,shape='circle')

    draw_turtle.penup()
    draw_turtle.setheading(30)
    for i in range(1,13):
        draw_turtle.fd(radius)
        draw_turtle.write(str(i),align="center",font=(None,18,"normal"))
        draw_turtle.bk(radius)
        draw_turtle.right(30)
    draw_turtle.showturtle()

def run_hour():
    """时钟指针"""
    hour = localtime(time()).tm_hour
    hour = hour % 12
    minute = localtime(time()).tm_min 
    second = localtime(time()).tm_sec 
    angle =  hour * 360/12  + (minute * 360/60 ) /12
    angle = angle % 360    
    turtle_hour.setheading(angle)
    turtle_hour.screen.ontimer(run_hour,60000)
       
def run_minute():
    """分钟指针"""
    minute = localtime(time()).tm_min          
    angle =   minute * 360/60 
    angle = angle % 360         
    turtle_minute.setheading(angle)
    turtle_minute.screen.ontimer(run_minute,1000)

def run_second():
    """秒钟指针"""
    second = localtime(time()).tm_sec 
    angle =  second * 360/60 
    angle = angle % 360         
    turtle_second.setheading(angle)
    turtle_second.screen.ontimer( run_second,1000)
    

You must log in to read the rest of this article. Please log in or register as a user.