import time |
from turtle import * |
|
|
def drawGap(): # 绘制数码管间隔 |
penup() # 提起笔移动,不绘制图形,用于另起一个地方绘制 |
fd( 5 ) # 向当前画笔方向移动 5 像素长度 |
|
|
def drawLine(draw): # 绘制单段数码管 |
drawGap() # 调用drawGap() ,绘制间隔 |
# turtle.pendown() 移动时绘制图形,缺省时也为绘制 |
pendown() if draw else penup() # draw不为0绘制图形,否则另起一个地方 |
fd( 40 ) # 画笔向当前方向绘制40像素长的线条 |
drawGap() # 调用drawGap(),绘制间隔 |
right( 90 ) # 顺时针移动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 ) |
left( 90 ) # 逆时针移动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 ) |
left( 180 ) # 逆时针移动180° |
penup() # 画笔另起一个新的点 |
fd( 20 ) # 沿当前方向移动20像素 |
|
|
def drawDate(date): |
pencolor( 'light coral' ) # 设置画笔的颜色为淡珊瑚色,默认初始值为黑色 |
for i in date: |
if not i.isnumeric(): |
sety( - 50 ) |
pencolor( 'wheat' ) |
if i = = '-' : |
write( '年' , font = ( '微软雅黑' , 24 , 'normal' )) |
pencolor( 'lightblue' ) # 设置画笔的颜色为“lightblue” |
fd( 45 ) |
elif i = = '=' : |
write( '月' , font = ( '微软雅黑' , 24 , 'normal' )) |
pencolor( 'lightpink' ) # 设置画笔的颜色为“lightpink” |
elif i = = '+' : |
write( '日' , font = ( '微软雅黑' , 24 , 'normal' )) |
else : |
drawDigit( eval (i)) |
sety( 0 ) |
|
|
if __name__ = = '__main__' : |
# 坐标位置(200,200)处绘制一个大小为 800*350像素的画布 |
setup( 800 , 350 , 200 , 200 ) |
screensize(bg = 'lightyellow' ) |
penup() # 开始绘制 |
hideturtle() # 绘制完日期,隐藏画笔的turtle形状 |
speed( 'fastest' ) |
fd( - 350 ) |
pensize( 5 ) # 定义画笔的大小为 5 |
# 使用strftime 方法将 gmtime() 获取的当前的日期以“%Y-%m=%d+”的格式赋值给变量a |
a = time.strftime( "%Y-%m=%d+" , time.gmtime()) |
drawDate(a) # 调用自定义函数 drawDate() |
done() # 必须是图形绘制程序的最后一个语句 |