python turtle库安装_Python之Turtle库示例应用

本实例原本是Mooc课程上的一个案例,稍加修改后可以显示实时时间

Python代码:

# -*- coding: utf-8 -*-

# @Author: Marte

# @Date:   2020-07-22 18:34:56

# @Last Modified by:   Marte

# @Last Modified time: 2020-07-23 13:52:15

# @Description: 七段数码管

import turtle as t

import time

def DrawLine(draw):

t.pendown() if draw else t.penup()

t.fd(30)

t.right(90)

def DrawDigit(digit):

DrawLine(True) if digit in [2,3,4,5,6,8,9] else DrawLine(False)

DrawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else DrawLine(False)

DrawLine(True) if digit in [0,2,3,5,6,8,9] else DrawLine(False)

DrawLine(True) if digit in [0,2,6,8] else DrawLine(False)

t.left(90)

DrawLine(True) if digit in [0,4,5,6,8,9] else DrawLine(False)

DrawLine(True) if digit in [0,2,3,5,6,7,8,9] else DrawLine(False)

DrawLine(True) if digit in [0,1,2,3,4,7,8,9] else DrawLine(False)

t.left(180)

t.penup()

t.fd(10)

def DrawDate(date):

t.pencolor("red")

for each in date:

if each == '-':

t.write("时",font=("Arial",15,"normal"))

t.pencolor("green")

t.fd(25)

elif each == '=':

t.write("分",font=("Arial",15,"normal"))

t.pencolor("blue")

t.fd(25)

elif each == '+':

t.write("秒",font=("Arial",15,"normal"))

t.fd(25)

else:

DrawDigit(eval(each))

def main(String):

t.title("七段数码管(动态刷新)")

t.setup(600,300)

t.penup()

t.hideturtle()

t.pensize(3)

while(True):

t.pencolor("black")

t.goto(-260,-35)

t.tracer(False)

t.write(String,font=("Arial",15,"normal"))

t.goto(-150,0)

# t.tracer(0)

timeString = time.strftime("%H-%M=%S+",time.localtime())

DrawDate(timeString)

print(timeString)

t.clear()

time.sleep(1)

if __name__ == '__main__':

T = time.gmtime()

dateString = time.strftime('%Y-%m-%d',T)

main(dateString)

运行结果:a14f0be831b7e797aa7647d0954b2fe0.png演示

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页