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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • module numpy has no attribute random_python attribute

    module numpy has no attribute random_python attribute介绍Python报错”AttributeError:module’numpy’hasnoattribute’unit8′”的解决方案

    2022年9月17日
    2
  • 深度学习–十折交叉验证

    深度学习–十折交叉验证用scikit-learn来评价模型质量,为了更好地挑拣出结果的差异,采用了十折交叉验证(10-foldcrossvalidation)方法。本程序在输入层和第一个隐含层之间加入20%Dropout采用十折交叉验证的方法进行测试。#dropoutintheinputlayerwithweightconstraintdefcreate_mode…

    2025年6月9日
    3
  • debian配置samba_ubuntu设置文件共享

    debian配置samba_ubuntu设置文件共享●安装与配置Samba共享服务●创建目录为/share/public的共享目录●共享名为public●仅支持匿名用户的只读访问(请在该目录内存放一个文件名为file.txt,文件内容为“TestFile”以便于测试)●创建目录为/share/files的共享目录●共享名为files●创建zhangsan,wangwu两个用户,密码均为Skills39,并且都能从client客户端登录访问到该共享目录并上传文件●zhangsan用户可以查看和删除所有人的文件●wa

    2022年9月16日
    1
  • django详解_java cookie

    django详解_java cookie前言cookie:在网站中,http请求是无状态的。也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户。cookie的出现就是为了解决这个问题,第一次登录

    2022年8月7日
    6
  • 目标检测与图像分割的区别_语义分割和实例分割最新论文

    目标检测与图像分割的区别_语义分割和实例分割最新论文计算机视觉的任务很多,有图像分类、目标检测、语义分割、实例分割和全景分割等,那它们的区别是什么呢?1、ImageClassification(图像分类)图像分类(下图左)就是对图像判断出所属的分类,比如在学习分类中数据集有人(person)、羊(sheep)、狗(dog)和猫(cat)四种,图像分类要求给定一个图片输出图片里含有哪些分类,比如下图的例子是含有person、sheep和do…

    2022年8月23日
    6
  • 常用数据库建模工具

    常用数据库建模工具收藏:http://www.oschina.net/project/tag/83/db-modelIntellij下mybatis插件 MyBatisCodeHelper [国产]MyBatisCodeHelper是Intellij上的mybatis插件,通过java对象生成建表sql,dao,mapperxml文件,方便的生成curd,另外可以通…上次更新:2017…

    2022年7月27日
    7

发表回复

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

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