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


相关推荐

  • gtest的介绍和使用

    gtest的介绍和使用一、什仫是gtestgtest是一个跨平台的(Liunx、MacOSX、Windows、Cygwin、WindowsCEandSymbian)C++单元测试框架,由google公司发布。gtest是为在不同平台上为编写C++测试而生成的。它提供了丰富的断言、致命和非致命判断、参数化、”死亡测试”等等。了解了什仫是gtest之后下面让我们来学习gt…

    2022年9月29日
    3
  • 我自己实际操作安装MyCat实现读写分离和遇到的一些问题

    我自己实际操作安装MyCat实现读写分离和遇到的一些问题

    2021年7月10日
    132
  • 3分钟告诉你如何成为一名黑客?|零基础到黑客入门指南,你只需要掌握这五点能力

    3分钟告诉你如何成为一名黑客?|零基础到黑客入门指南,你只需要掌握这五点能力三分钟带各位揭秘黑客究竟是什么,以及想要成为黑客都需要具备哪些能力?

    2022年6月4日
    49
  • javascript中一个字符占几个字节

    javascript中一个字符占几个字节一般来说英文是1个,中文是两个。但是会根据编码方式不同而不同。以下是搬运:英文字母和中文汉字在不同字符集编码下的字节数英文字母:字节数:1;编码:GB2312字节数:1;编码:GBK字节数:1;编码:GB18030字节数:1;编码:ISO-8859-1字节数:1;编码:UTF-8字节数:4;编码:UTF-16字节数:2;编码:UTF-16BE字节数…

    2025年5月31日
    2
  • 用U盘ghost备份win10系统操作流程_ghost全盘备份还原

    用U盘ghost备份win10系统操作流程_ghost全盘备份还原使用pe进行备份1、这里我使用了老毛桃的pe系统2、备份分区3、选中要备份的硬盘4、选择备份的分区按下shift按键,可以同时选中EFISystem分区和C盘系统分区。5、选择保存位置选择保存的位置,以及文件名称,这里最好保存到一个文件夹下,因为备份的文件可能会是多个。6、等待备份完成还原系统1、新电脑上建立分区选中空硬盘,点击新建分区,最后建立出来ESP\MSR\系统分区。在建立ESP分区的时候可以分配一个盘符,否则还原系统时,可能看不到这个分区。2、开始

    2025年9月20日
    4
  • Java将Map数据转换为JSON对象数据

    Java将Map数据转换为JSON对象数据//pom.xml导入jar包<!–拼接json–><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</versi…

    2022年5月6日
    110

发表回复

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

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