python数码管动态时钟_Python绘制数码管显示当前时间

利用Python中的turtle图形库绘制七段数码管,显示当前时间

代码

# coding:utf-8

# 绘制七段数码管,显示当前时间

import time

import turtle as tt

# 绘制间隔

def drawGap():

tt.penup()

tt.fd(5)

# 绘制单段数码管

def drawLine(draw):

drawGap()

if(draw):

tt.pendown()

else:

tt.penup()

tt.fd(50)

drawGap()

tt.right(90)

# 绘制当前时间

def drawDate(date):

tt.pencolor("red")

for i in date:

if i == '+':

tt.write("年", font = ("黑体", 25, "normal"))

tt.pencolor("green")

tt.fd(50)

elif i == '-':

tt.write("月", font=("黑体", 25, "normal"))

tt.pencolor("blue")

tt.fd(50)

elif i == '*':

tt.write("日", font=("黑体", 25, "normal"))

tt.pencolor("purple")

tt.fd(50)

elif i == '=':

tt.write("时", font=("黑体", 25, "normal"))

tt.pencolor("yellow")

tt.fd(50)

elif i == '#':

tt.write("分", font=("黑体", 25, "normal"))

tt.pencolor("black")

tt.fd(50)

elif i == '$':

tt.write("秒", font=("黑体", 25, "normal"))

else:

drawDigit(eval(i))

# 绘制数码管

def drawDigit(cur_time):

if cur_time in [2, 3, 4, 5, 6, 8, 9]:

drawLine(True)

else:

drawLine(False)

if cur_time in [0, 1, 3, 4, 5, 6, 7, 8, 9]:

drawLine(True)

else:

drawLine(False)

if cur_time in [0, 2, 3, 5, 6, 8, 9]:

drawLine(True)

else:

drawLine(False)

if cur_time in [0, 2, 6, 8]:

drawLine(True)

else:

drawLine(False)

tt.left(90)

if cur_time in [0, 4, 5, 6, 8, 9]:

drawLine(True)

else:

drawLine(False)

if cur_time in [0, 2, 3, 5, 6, 7, 8, 9]:

drawLine(True)

else:

drawLine(False)

if cur_time in [0, 1, 2, 3, 4, 7, 8, 9]:

drawLine(True)

else:

drawLine(False)

tt.left(180)

tt.penup()

tt.fd(20)

# 主函数

def main():

tt.setup(1600, 300, 200, 200)

tt.penup()

tt.fd(-730)

tt.pensize(5)

drawDate(time.strftime('%Y+%m-%d*%H=%M#%S$', time.localtime()))

tt.hideturtle()

tt.done()

if __name__ == "__main__":

main()

结果

总结

通过在mooc网学习相关知识,借鉴相关代码,最终实现本次设计。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值