本文主要是介绍Python之七段数码管绘制问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
来源:Python语言程序设计 -嵩天
一、问题描述
如下所示的七段数码管绘制时间(年月日)
二、代码实现
"""
七段数码管绘制年月日时间
"""import turtle
import timedef drawLine(draw):
# 绘制单端数码管turtle.pendown() if draw else turtle.penup()turtle.fd(40)turtle.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)turtle.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)turtle.left(180)turtle.penup()turtle.fd(20)# 为绘制后续数字确定起始位置def drawData(data):turtle.pencolor("red")for i in str(data):if i == "-":turtle.write("年",font=("Arial",18,"normal"))turtle.pencolor("green")turtle.fd(40)elif i == "=":turtle.write("月",font=("Arial",18,"normal"))turtle.pencolor("blue")turtle.fd(40)elif i == "+":turtle.write("日",font=("Arial",18,"normal"))else: drawDigit(eval(i))def main():turtle.setup(800,350,200,200)# 画布大小turtle.penup()turtle.fd(-300)turtle.pensize(5)data = time.strftime("%Y-%m=%d+",time.gmtime())drawData(data)turtle.hideturtle()# 隐藏turtle(即箭头)turtle.done()# 给用户时间来查看图形,没有这行,图形窗口会在程序完成是立即关闭。main()
这篇关于Python之七段数码管绘制问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!