电子时钟(python)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

电子时钟
代码如下:
import turtle as t
from datetime import datetime
def drawGap():
t.penup()
t.fd(5)
def drawLine(draw):
drawGap()
t.pendown() if draw else t.penup()
t.fd(40)
drawGap()
t.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)
t.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)
t.left(180)
t.penup()
t.fd(20)
def drawDate(date):
t.pencolor('red')
for i in date:
if i == '-':
t.write('时', font=('Arial', 18, 'normal'))
t.pencolor('green')
t.fd(40)
elif i == '=':
t.write('分', font=('Arial', 18, 'normal'))
t.pencolor('blue')
t.fd(40)
elif i == '+':
t.write('秒', font=('Arial', 18, 'normal'))
else:
drawDigit(eval(i))
def tick():
t.clear()
t.penup()
t.color('red')
t.goto(-150, 100)
t.pendown()
t.write('珍惜每一分善用每一秒', font=('Arial', 18, 'normal')) t.penup()
t.home()
t.fd(-300)
# 获取当前时间
drawDate(datetime.now().strftime("%H-%M=%S+"))
t.hideturtle()
# 定时器,一秒执行一次
t.ontimer(tick, 1000)
if __name__ == "__main__":
t.title("电子时钟")
# 设置窗口大小
t.setup(800, 350, 200, 200)
# 隐藏绘图过程
t.tracer(False)
t.pensize(8)
tick()
t.done()
实现效果:。

相关文档
最新文档