Python七段数码管显⽰当前时间import turtle,time
def drawGap(): #定义⼀个空⾏函数
turtle.penup()
turtle.forward(6)
def drawLine(draw): #定义数码管⼀个管的绘制函数
drawGap()
if draw:
turtle.pendown()
el:
turtle.penup()
turtle.forward(60)海尔洗衣机怎么样
元旦绘画作品
drawGap()
turtle.right(90)
def drawDigit(digit): #定义⼀个数字的绘制函数,if-el使⽤了两种⽅法。
#七段数码管第⼀段管
if digit in [2,3,4,5,6,8,9]:
drawLine(True)
el:
drawLine(Fal)
#七段数码管第⼆段管
if digit in [0,1,3,4,5,6,7,8,9]:
drawLine(True)
el:
drawLine(Fal)
前鼻韵母和后鼻韵母
#七段数码管第三段管
吧主if digit in [0,2,3,5,6,8,9]:
drawLine(True)
el:
drawLine(Fal)
#七段数码管第四段管
if digit in [0,2,6,8]:
drawLine(True)
el:
盐酸索他洛尔片
drawLine(Fal)
turtle.left(90)
# 七段数码管第五段管
drawLine(True) if digit in [0,4,5,6,8,9] el drawLine(Fal)
# 七段数码管第六段管
drawLine(True) if digit in [0,2,3,5,6,7,8,9] el drawLine(Fal)
# 七段数码管第七段管
drawLine(True) if digit in [0,1,2,3,4,7,8,9] el drawLine(Fal)
turtle.left(180)
turtle.penup()
turtle.forward(30)
def drawDate(date): #定义绘制函数,通过格式化时间,简化年⽉⽇汉⼦的输出。
for i in date:
turtle.pencolor("red")
turtle.speed(9)容忍与自由
if i=="-":
turtle.pencolor("blue")
turtle.write("年",font=("Arial",22,"normal"))
turtle.forward(40)
elif i=="=":
turtle.pencolor("green")
turtle.write("⽉",font=("Arial",22,"normal"))
turtle.forward(40)
elif i == "+":
turtle.pencolor("yellow")
turtle.pencolor("yellow")
turtle.write("⽇", font=("Arial", 22, "normal"))
el:
drawDigit(eval(i))
def main(num): #主函数
方单turtle.tup(1000,350,200,200)
turtle.pensize(6)
turtle.penup()
虚实turtle.forward(-450)
drawDate(num)
turtle.hideturtle()
turtle.done()
def getTime(): #定义时间获取函数
t = ime()
tt=time.strftime("%Y-%m=%d+", t)
return tt
num=getTime()
main(num)
输出:
继续优化上⾯的代码,减少复杂的地⽅,将显⽰当前的时间范围扩⼤到时分秒,⽽且是本地时间,代码如下:
import turtle
import time
def drawGap(): #设置⼀个画笔抬起,使得每段管之间产⽣间隔
turtle.penup()
turtle.forward(5)
def drawLine(draw):
drawGap()
turtle.pendown() if draw el turtle.penup()
turtle.forward(40)
drawGap()
turtle.right(90)
def drawDigit(digit):
drawLine(True) if digit in [2,3,4,5,6,8,9] el drawLine(Fal)
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] el drawLine(Fal)
drawLine(True) if digit in [0,2,3,5,6,8,9]el drawLine(Fal)
drawLine(True) if digit in [0,2,6,8] el drawLine(Fal)
turtle.left(90)
drawLine(True) if digit in [0,4,5,6,8,9] el drawLine(Fal)
drawLine(True) if digit in [0,2,3,5,6,7,8,9] el drawLine(Fal)
drawLine(True) if digit in [0,2,3,5,6,7,8,9] el drawLine(Fal)
drawLine(True) if digit in [0,1,2,3,4,7,8,9] el drawLine(Fal)
turtle.left(180)
turtle.penup()
turtle.forward(20)
def drawDate(date):
for i in date:
turtle.pencolor("red")
turtle.speed(10)
if i=="-":
turtle.write("年",font=("Arial",30,"normal"))
turtle.forward(40)
elif i=="=":
turtle.write("⽉",font=("Arial",30,"normal"))
turtle.forward(40)
elif i == "+":
turtle.write("⽇", font=("Arial", 30, "normal"))
elif i == "!":
turtle.penup()
turtle.forward(100)
elif i == "@":
turtle.write("时", font=("Arial", 30, "normal"))
turtle.forward(40)
elif i == "#":
turtle.write("分", font=("Arial", 30, "normal"))
turtle.forward(40)
elif i == "¥":
turtle.write("秒", font=("Arial", 30, "normal"))
el:
drawDigit(eval(i))
def main(num):
turtle.tup(1400,300,200,200)
turtle.pensize(5)
turtle.penup()
turtle.forward(-650)
drawDate(num)
turtle.hideturtle()
turtle.done()
def getTime():
t = time.localtime() #获取本地时间
tt=time.strftime("%Y-%m=%d!%H@%M#%S¥", t) #格式化本地时间输出 return tt
num=getTime()
main(num)
输出: