实验3 利用python的标准库实现简易的时钟

实验三:python标准库的使用

前言

  1. 掌握Turtle库的使用
  2. 掌握Datetime库的使用

正文

利用Turtle和Datetime库函数实现时钟显示功能

编写程序,利用Turtle和Datetime库函数,实现时钟显示的功能,要求显示界面如下图所示。请编写测试代码。

在这里插入图片描述

# 代码实现
import datetime
import turtle


# 此函数作用:抬笔,跳到一个地方
def skip(setp) :
    # 提起画笔
    turtle.penup ()
    # 向当前画笔方向移动distance像素长度
    turtle.forward ( setp )
    # 放下画笔
    turtle.pendown ()


# 此函数作用:画表盘
def createClock(radius) :
    # 设置画笔的移动速度,范围为[0,10]的整数,数字越大,移动速度越快
    turtle.speed ( 0 )
    # 以logo坐标、角度方式
    turtle.mode ( "logo" )
    # 隐藏当前画笔的turtle形状
    turtle.hideturtle ()
    # 设置画笔的宽度
    turtle.pensize ( 7 )
    # 回到原点
    turtle.home ()
    for j in range ( 60 ) :
        skip ( radius )
        if j % 5 == 0 :
            turtle.forward ( 20 )
            skip ( -radius - 20 )
        else :
            turtle.dot ( 5 )
            skip ( -radius )
        # 顺时针方向移动angle度
        turtle.right ( 6 )


# 此函数作用:创建钟的指针(时针、分针、秒针)
def createHand(name , length) :
    # 清空当前的窗口,并重置位置等状态为默认值
    # turtle.reset()
    # 提起画笔
    turtle.penup ()
    turtle.home()
    # 从指针的当前位置开始记录绘制图形的顶点
    turtle.begin_poly ()
    turtle.back ( 0.1 * length )
    # 向当前画笔方向移动distance像素单位
    turtle.forward ( length * 1.1 )
    # 停止记录多边形的顶点
    turtle.end_poly ()
    # 返回最后绘制图形的所有坐标,只记录每次绘图的起始点坐标和终止点坐标
    poly = turtle.get_poly ()
    # 注册为一个shape
    turtle.register_shape ( name , poly )


# 此函数作用:画指针
def drawHand() :
    global hour_hand , minute_hand , second_hand , font_Writer
    createHand ( 'hour_hand' , 100 )
    createHand ( 'minute_hand' , 120 )
    createHand ( 'second_hand' , 140 )

    hour_hand = turtle.Pen ()
    hour_hand.shape ( 'hour_hand' )
    hour_hand.shapesize ( 1 , 1 , 6 )

    minute_hand = turtle.Pen ()
    minute_hand.shape ( 'minute_hand' )
    minute_hand.shapesize ( 1 , 1 , 4 )

    second_hand = turtle.Pen ()
    second_hand.shape ( 'second_hand' )
    second_hand.shapesize ( 1 , 1 , 2 )
    second_hand.pencolor ( 'pink' )

    font_Writer = turtle.Pen ()
    font_Writer.pencolor ( 'gray' )
    font_Writer.hideturtle ()


def getWeek(weekday) :
    # datatime.date.weekday():返回日期的星期,星期一,返回0;星期二,返回1
    weekName = ['星期一' , '星期二' , '星期三' , '星期四' , '星期五' , '星期六' , '星期天']
    return weekName[weekday]


def getDate(year , month , day) :
    return "%s-%s-%s" % (year , month , day)


def getRealtime() :
    # datatime.datatime类:datatime类可以看成是data类和time类的集合
    # 返回当前系统的日期时间
    current = datetime.datetime.now ()
    current_year = current.year
    current_month = current.month
    current_day = current.day
    current_hour = current.hour
    current_minute = current.minute
    current_second = current.second
    current_weekday = current.weekday ()
    turtle.tracer ( False )

    # setheading(angle):设置当前朝向angle的角度
    second_hand.setheading ( 360 / 60 * current_second )
    minute_hand.setheading ( 360 / 60 * current_minute )
    hour_hand.setheading ( 360 / 12 * current_hour + 30 / 60 * current_minute )

    # 清空当前的窗口,但不改变画笔的位置
    font_Writer.clear ()

    font_Writer.home ()
    # 提起画笔
    font_Writer.penup ()
    # 向当前画笔方向移动distance像素长度
    font_Writer.forward ( 80 )

    #     用turtle写文字
    font_Writer.write ( getWeek ( current_weekday ) , align='center' , font=('Courier' , 14 , "bold") )
    font_Writer.forward ( -160 )
    font_Writer.write ( getDate ( current_year , current_month , current_day ) , align='center' ,
                        font=('Courier' , 14 , 'bold') )
    turtle.tracer ( True )
    # print(current_second)
    # 每隔1000毫秒调用一次getRealtime()
    turtle.ontimer ( getRealtime , 1000 )


def main() :
    turtle.tracer ( False )
    createClock ( 160 )
    drawHand ()
    getRealtime ()
    turtle.tracer ( True )
    turtle.mainloop ()


if __name__ == '__main__' :
    main ()

运行结果如下图所示:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&晨曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值