python编写时钟代码

python编写时钟代码#!/urs/bin/python#_*_coding:utf-8#!/usr/bin/envpython#-*-coding:utf-8-*-importturtleimportdatetime#移动一段距离defskip(distance):”””移动乌龟一段距离,不留痕迹:param…

大家好,又见面了,我是你们的朋友全栈君。

python编写时钟代码

# !/urs/bin/ python
# _*_ coding: utf-8
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import turtle
import datetime


# 移动一段距离
def skip(distance):
    """
    移动乌龟一段距离,不留痕迹
    :param distance: 像素
    :return:
    """
    turtle.penup()
    turtle.forward(distance)
    turtle.pendown()


def draw_clock():
    # 先画表盘
    # 先画点
    # 移动一段距离,画一个点,然后退回
    # 转动6°,再移动一段距离,画一个点,然后退回
    # 循环 60次
    # 让乌龟的方向默认向上
    turtle.reset()
    turtle.hideturtle()
    for i in range(60):

        skip(160)
        # 根据 5格一个时钟
        if i % 5 == 0:
            turtle.pensize(7)
            # 画时钟
            turtle.forward(20)
            if i == 0:
                turtle.write(12, align='center', font=('Courier', 14, 'bold'))
            elif i == 25 or i == 30 or i == 35:
                skip(25)
                turtle.write(int(i / 5), align='center', font=('Courier', 14, 'bold'))
                skip(-25)

            else:
                turtle.write(int(i / 5), align='center', font=('Courier', 14, 'bold'))
            skip(-20)
        else:
            turtle.pensize(1)
            turtle.dot()
        skip(-160)
        turtle.right(6)


def get_week(t):
    week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    return week[t.weekday()]


def create_hand(length, name):
    turtle.reset()
    skip(-length * 0.1)
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    turtle.end_poly()
    # 注册
    turtle.register_shape(name, turtle.get_poly())
    hand = turtle.Turtle()
    hand.shape(name)
    hand.shapesize(1, 1, 3)
    return hand


def run():
    # 不停的获取时间
    t = datetime.datetime.today()
    bob.forward(65)
    bob.write(get_week(t), align='center', font=('Courier', 14, 'bold'))
    bob.back(130)
    bob.write(t.strftime('%Y-%m-%d'), align='center', font=('Courier', 14, 'bold'))
    bob.home()
    # 指针移动
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60
    hour = t.hour + minute / 60
    turtle.tracer(True)
    second_hand.setheading(6 * second)
    minute_hand.setheading(6 * minute)
    hour_hand.setheading(30 * hour)
    turtle.ontimer(run, 200)


if __name__ == '__main__':
    # 画秒针,分针,时针
    turtle.mode('logo')
    turtle.hideturtle()
    global second_hand, minute_hand, hour_hand, bob

    second_hand = create_hand(135, 'second_hand')
    minute_hand = create_hand(125, 'minute_hand')
    hour_hand = create_hand(90, 'hour_hand')
    # 创建一个新的turtle对象,去循环的操作
    bob = turtle.Turtle()
    bob.hideturtle()
    bob.penup()

    turtle.tracer(False)
    draw_clock()
    run()

    turtle.mainloop()

  

转载于:https://www.cnblogs.com/springcloud/p/8624040.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154117.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • axure菜单展开收起_css菜单栏的隐藏和显示

    axure菜单展开收起_css菜单栏的隐藏和显示这样可以实现菜单栏最小化,而且在你鼠标不移动到左上角时,小箭头会隐藏,效果就可以了。axure9.0版本在发布后HTML页面打开时总是在顶部弹出菜单既不美观也影响效果。本人axure小白,摸索半天后发现也不能完全关闭或者不显示(除非代码修改);解决方案就是在请求地址后面拼接。…

    2022年8月19日
    16
  • dialog使用案例,包含增加按钮关闭弹窗

    dialog使用案例,包含增加按钮关闭弹窗

    2021年7月20日
    65
  • Linux Shell循环结构

    Linux Shell循环结构fi}&donewaitecho“MissionCompleted.”*for循环语句实现文件中批量用户创建“`shell[root@hdp-1shell]#catusers_from_file.shIFS=$’\n’forlinein`cat$1`doif[${#line}-eq0];thencontinuefiuser=`echo”$line”|awk'{print$1}’`pass=`echo”$line”|a

    2022年7月24日
    11
  • mysql 数据库面试题[通俗易懂]

    mysql 数据库面试题[通俗易懂]mysql 数据库面试题

    2022年4月24日
    46
  • 模型矩阵、视图矩阵、投影矩阵

    模型矩阵、视图矩阵、投影矩阵模型视图投影矩阵的作用,就是将顶点从局部坐标系转化到规范立方体(CanonicalViewVolnme)中。总而言之,模型视图投影矩阵=投影矩阵×视图矩阵×模型矩阵,模型矩阵将顶点从局部坐标系转化到世界坐标系中,视图矩阵将顶点从世界坐标系转化到视图坐标系下,而投影矩阵将顶点从视图坐标系转化到规范立方体中。如下图所示,假设现在要将三维空间中的三角形渲染到屏幕上。三角形的模型文件中,顶点坐标是…

    2022年5月14日
    79
  • 51单片机毕业设计题目_51单片机经典项目

    51单片机毕业设计题目_51单片机经典项目STC12C5A60S2DS12887单片机毕业设计51单片机项目数码管显示的合成出租车计价器设计Synthetictaximeterdigitaldisplaydesign学生姓名: 学生学号: 10700121专业名称: 电子信息工程指导教师:计算机与信息工程学院2014年6月13日独创性声明本人声明所呈交的毕业设计(论文)是本人在指导教师指导下进行的研究工作和取得的研究成果,除了文中特别加以引用标注之处外,论文中不包含其他人已经发表或撰写过的研究成果,没有伪造数据的

    2022年10月3日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号