首页 > 编程语言 >Python模拟时钟演示及源代码

Python模拟时钟演示及源代码

时间:2024-06-09 21:30:31浏览次数:15  
标签:turtle Turtle printer 演示 Python skip 源代码 houHand def

 


turtle 是Python中的一个模块,用于绘图和图形设计。它提供了一个简单的绘图窗口,可以绘制各种形状、线条和颜色等。通过使用turtle模块,我们可以在屏幕上实时地绘制图形,并且可以控制画笔的移动、旋转等操作。


 

2、使用示例 下面是一个简单的使用turtle模块绘制一个正方形的示例:

python复制代码运行

import turtle

# 创建一个turtle对象
t = turtle.Turtle()

# 绘制正方形
for i in range(4):
    t.forward(100)  # 向前移动100个单位
    t.right(90)     # 向右转90度

# 结束绘制
turtle.done()

3、注意事项

  • 在使用turtle模块时,需要确保已经安装了该模块。如果没有安装,可以使用pip install turtle命令进行安装。
  • turtle模块的绘图速度可能会比较慢,因为它是基于屏幕刷新的方式进行绘制的。如果需要提高绘图速度,可以尝试使用其他更高效的绘图库,如Pygame或PIL(Python Imaging Library)。

这篇文章是你的 Python 工具箱,你可以在其中复制粘贴代码到你的项目中,所以收藏好它,并开始使用Python吧。

源代码笔记:

import turtle
from datetime import *

# 抬起画笔,向前运动一段距离放下
def skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()

def mkHand(name, length):
    # 注册Turtle形状,建立表针Turtle
    turtle.reset()
    # 先反向运动一段距离,终点作为绘制指针的起点
    skip(-length * 0.1)
    # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
    turtle.end_poly()
    # 返回最后记录的多边形。
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)

def init():
    global secHand, minHand, houHand, printer
    # 重置Turtle指向北
    turtle.mode("logo")
    # 建立三个表针Turtle并初始化
    mkHand("secHand", 135)
    mkHand("minHand", 125)
    mkHand("houHand", 90)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    houHand = turtle.Turtle()
    houHand.shape("houHand")

    for hand in secHand, minHand, houHand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)

    # 建立输出文字Turtle
    printer = turtle.Turtle()
    # 隐藏画笔的turtle形状
    printer.hideturtle()
    printer.penup()

# 绘制表盘
def setupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.pensize(7)
    for i in range(60):
        # 向前移动半径值
        skip(radius)
        if i % 5 == 0:
            # 画长刻度线
            turtle.forward(20)
            # 回到中心点
            skip(-radius - 20)

            # 移动到刻度线终点
            skip(radius + 20)
            # 下面是写表盘刻度值,为了不与划线重叠,所以对于特殊位置做了处理
            if i == 0:
                turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
            elif i == 30:
                skip(25)
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
                skip(-25)
            elif (i == 25 or i == 35):
                skip(20)
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
                skip(-20)
            else:
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
            
            # 回到中心点
            skip(-radius - 20)
        else:
            # 画圆点
            turtle.dot(5)
            skip(-radius)
        # 顺时针移动6°
        turtle.right(6)

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

def date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s %d %d" % (y, m, d)

def tick():
    # 绘制表针的动态显示
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    houHand.setheading(30 * hour)

    turtle.tracer(False)
    printer.forward(65)
    printer.write(week(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.back(130)
    printer.write(date(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.home()
    turtle.tracer(True)

    # 100ms后继续调用tick
    turtle.ontimer(tick, 100)

def main():
    # 打开/关闭龟动画,并为更新图纸设置延迟。
    turtle.tracer(False)
    init()
    setupClock(160)
    turtle.tracer(True)
    tick()
    turtle.mainloop()

if __name__ == "__main__":
    main()

效果图: 

 

 

标签:turtle,Turtle,printer,演示,Python,skip,源代码,houHand,def
From: https://blog.csdn.net/m0_58552717/article/details/139562055

相关文章

  • python gdal 安装使用(Windows, python 3.6.8)
    pythongdal安装使用pythonGDAL有两种安装方式:第一种是利用pipinstallgdal安装如果安装失败,可以采用下面的方法:第二种离线安装步骤:(1)查看python版本;(2)下载gdal的whl文件;(3)利用pipinstall下载的gdal.whl文件;(4)将gdal中的可执行文件所在路径添加到系统环境中;具体操作见......
  • Python-金融编程第二版-GPT-重译--一-
    Python金融编程第二版(GPT重译)(一)原文:annas-archive.org/md5/d2f94efd019a2e2cb5c4fa9f260d63c译者:飞龙协议:CCBY-NC-SA4.0第一部分:Python与金融本部分介绍了Python在金融领域的应用。它包括三章:第一章简要讨论了Python的一般情况,并论述了为什么Python确实非常......
  • Python-金融编程第二版-GPT-重译--四-
    Python金融编程第二版(GPT重译)(四)原文:annas-archive.org/md5/d2f94efd019a2e2cb5c4fa9f260d63c译者:飞龙协议:CCBY-NC-SA4.0第八章:金融时间序列时间的唯一目的是让一切不是同时发生。阿尔伯特·爱因斯坦金融时间序列数据是金融领域最重要的数据类型之一。这是按日期和/......
  • Python-金融编程第二版-GPT-重译--三-
    Python金融编程第二版(GPT重译)(三)原文:annas-archive.org/md5/d2f94efd019a2e2cb5c4fa9f260d63c译者:飞龙协议:CCBY-NC-SA4.0第六章:面向对象编程软件工程的目的是控制复杂性,而不是创建它。PamelaZave介绍面向对象编程(OOP)是当今最流行的编程范式之一。正确使用时,它与过......
  • Python-金融编程第二版-GPT-重译--二-
    Python金融编程第二版(GPT重译)(二)原文:annas-archive.org/md5/d2f94efd019a2e2cb5c4fa9f260d63c译者:飞龙协议:CCBY-NC-SA4.0第四章:使用NumPy进行数值计算计算机是无用的。它们只能给出答案。巴勃罗·毕加索介绍本章介绍了Python的基本数据类型和数据结构。尽管Py......
  • The Imitate Google Dinosaur for the python <--CSDN @PythonWIN-->
    - Thisisasmallsoftwarebasedonpygame:    -Wehaveusedthewell-knownPythonlanguage-Iamcurrentlydeveloping ImitateGoogleDinosaurversion1.1andusingPythonasmyeditor.Nowletmeexplainindetailitsfunctions:    -F......
  • The Table Pet for the python <--CSDN @PythonWIN-->
    - ThisisasmallsoftwarebasedonpyQT5:    -Wehaveusedthewell-knownPythonlanguage-IamcurrentlydevelopingdesktoppetSVIPversion1.1andusingPythonasmyeditor.Nowletmeexplainindetailitsfunctions:    -First,Anim......
  • python 字典是不是线程安全的
    Python字典(dict)对象本身不是线程安全的。在多线程环境下,对同一个字典对象的读写操作需要额外的同步机制来确保线程安全性。如果需要在多线程环境下使用线程安全的字典,可以使用collections.Counter对象,它是线程安全的,或者使用threading.local,它提供了线程局部存储的功能。另外......
  • python 调用 outlook(全网功能最全)
    Outlook文章目录一、调用win32com操作outlook发送邮件总结一、调用win32com操作outlook发送邮件需要为你的电脑安装outlook代码如下:fromwin32com.clientimportDispatch#导入包classoutlook():'''重写outlook邮件功能'''def__init__(......
  • VSCode 1.90版本 升级需谨慎~(Python)
        由于再次出现justMyCode的问题,请使用Python的小伙伴要谨慎将VisualStudioCode升级到1.90版本(两天前官方刚发布的) 若已经升级到1.90的小伙伴,可以从这里下载1.89版本进行“补救“:    1.89.1版本下载链接: https://code.visualstudio.com/updates/v1_89    ......