这是我今天通过上一段程序举一反三的,也就是改了表示的码最后得到了一个小时,分钟,秒为单位的程序。
语法是还是和上一个程序一样的就是重点介绍后面呈现代码的刷新。
首先我们先看下代码
#七段数码管的绘制.py
import turtle as t
import time
import datetime
from random import *
def drawGap(): # 绘制数码管间隔
t.penup()
t.fd(10)
def drawLine(draw): # 绘制单段数码管
drawGap()
t.pendown() if draw else t.penup()
t.fd(40)
drawGap()
t.right(90)
def drawDight(dight): # 根据数字绘制七段数码管
t.pencolor(random(), random(), random())
drawLine(True) if dight in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
t.pencolor(random(), random(), random())
drawLine(True) if dight in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)
t.pencolor(random(), random(), random())
drawLine(True) if dight in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
t.pencolor(random(), random(), random())
drawLine(True) if dight in [0, 2, 6, 8] else drawLine(False)
t.pencolor(random(), random(), random())
t.left(90)
drawLine(True) if dight in [0, 4, 5, 6, 8, 9] else drawLine(False)
t.pencolor(random(), random(), random())
drawLine(True) if dight in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
t.pencolor(random(), random(), random())
drawLine(True) if dight in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
t.pencolor(random(), random(), random())
t.left(180)
t.penup() # 为绘制后续数字确定位置
t.fd(20) # 为绘制后续数字确定位置
def drawDate(date):
for i in date:
if i == '-':
t.write('时', font=("Arial", 28, "normal"))
t.fd(40)
elif i == '=':
t.write('分', font=("Arial", 28, "normal"))
t.fd(40)
elif i == '+':
t.write('秒', font=("Arial", 28, "normal"))
t.fd(40)
else:
drawDight(eval(i))
def main():
while True:
t.reset()
#清空窗口,重置turtle状态为起止状态
t.setup(800, 350, 200, 200)
t.penup()
t.fd(-300)
t.pensize(5)
t.tracer(False) # 隐藏绘图,直接显示结果
t.hideturtle()
drawDate(datetime.datetime.now().strftime('%S+%M=%H-')) # 将模板设置为"小时-分钟=秒+"
time.sleep(1)
t.done()
main()
和之前不同的事增加了t.reset()清空窗口内容和t.tracer(False)跳过了绘图过程,直接显示出结果。主要是有个刷新的过程。
最后大家看下呈现出来的结果:
五颜六色还会跟着刷新变换颜色。如此有趣的代码还不快去试一试吧。