【python】 turtle显示当前年月日

【python】 turtle显示当前年月日

import turtle,datetime
def drawline(draw):
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    turtle.right(90)
def drawdigit(d):
    drawline(True) if d in [2, 3, 4, 5, 6, 8, 9] else drawline(False)
    drawline(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawline(False)
    drawline(True) if d in [0, 2, 3, 5, 6, 8, 9] else drawline(False)
    drawline(True) if d in [0, 2, 6, 8] else drawline(False)
    turtle.left(90)
    drawline(True) if d in [0, 4, 5, 6, 8, 9] else drawline(False)
    drawline(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else drawline(False)
    drawline(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else drawline(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)
def drawdate(date):
    for i in date:
        drawdigit(eval(i))
def main():
    turtle.setup(800, 350, 200, 200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawdate(datetime.datetime.now().strftime('%Y%m%d'))
    turtle.hideturtle()
main()

运行结果

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供代码示例,以下是基于 turtle 库和 datetime 库完成七段数码管绘制的代码: ```python import turtle import datetime # 设置窗口大小 turtle.setup(800, 600) # 设置画笔 turtle.penup() turtle.speed(0) turtle.pensize(5) # 数字对应的七段数码管 digit_segments = { 0: [1, 1, 1, 0, 1, 1, 1], 1: [0, 0, 1, 0, 0, 1, 0], 2: [1, 0, 1, 1, 1, 0, 1], 3: [1, 0, 1, 1, 0, 1, 1], 4: [0, 1, 1, 1, 0, 1, 0], 5: [1, 1, 0, 1, 0, 1, 1], 6: [1, 1, 0, 1, 1, 1, 1], 7: [1, 0, 1, 0, 0, 1, 0], 8: [1, 1, 1, 1, 1, 1, 1], 9: [1, 1, 1, 1, 0, 1, 1] } # 绘制七段数码管 def draw_segment(seg, x, y): turtle.setpos(x, y) turtle.pendown() if seg == 1: turtle.setheading(90) turtle.forward(50) elif seg == 0: turtle.setheading(0) turtle.forward(50) turtle.setheading(90) turtle.forward(50) turtle.setheading(180) turtle.forward(50) turtle.setheading(-90) turtle.forward(50) turtle.setheading(0) turtle.forward(50) turtle.penup() # 绘制数字 def draw_digit(digit, x, y): segments = digit_segments[digit] draw_segment(segments[0], x + 10, y + 60) draw_segment(segments[1], x + 60, y + 10) draw_segment(segments[2], x + 110, y + 10) draw_segment(segments[3], x + 160, y + 60) draw_segment(segments[4], x + 110, y + 110) draw_segment(segments[5], x + 60, y + 110) draw_segment(segments[6], x + 10, y + 60) # 获取当前时间 now = datetime.datetime.now() # 获取小时、分钟、秒钟 hour = now.hour minute = now.minute second = now.second # 绘制数字时分秒 draw_digit(hour // 10, -200, 0) draw_digit(hour % 10, -90, 0) draw_digit(minute // 10, 30, 0) draw_digit(minute % 10, 140, 0) draw_digit(second // 10, 250, 0) draw_digit(second % 10, 360, 0) # 隐藏画笔 turtle.hideturtle() # 等待用户关闭窗口 turtle.done() ``` 这段代码会绘制当前时间的时分秒,如果您想要绘制其他数字,可以修改 `hour`、`minute`、`second` 的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值