本文主要是介绍Python七彩管实现某个时刻的倒计时(如:北京奥运会倒计时),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import turtle, time# 七彩管间距
def drawgap():turtle.penup()turtle.fd(5)# 绘制单根七彩管
def drawline(draw):drawgap()turtle.pendown() if draw else turtle.penup()turtle.fd(40)drawgap()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 drawdate(date):turtle.pencolor('red')for i in date:if i == '=':turtle.write('天', font=('Arial', 18, 'normal'))turtle.pencolor('purple')turtle.fd(40)elif i == '+':turtle.write('时', font=('Arial', 18, 'normal'))turtle.pencolor('brown')turtle.fd(40)elif i == '!':turtle.write('分', font=('Arial', 18, 'normal'))turtle.fd(40)elif i == '*':turtle.write('秒', font=('Arial', 18, 'normal'))turtle.fd(40)else:drawdigit(eval(i))def main():turtle.setup(800, 350, 200, 200)turtle.pensize(5)turtle.hideturtle()t = (2021, 4, 10, 9, 30, 0, 6, 100, 1) # 自考时间 2021年04月10日09:30:00while True:turtle.clear()turtle.penup()turtle.setpos(-396, 0)turtle.tracer(False)drawdate(time.strftime('%j=%H+%M!%S*', time.gmtime(time.mktime(t) - time.time())))turtle.hideturtle()time.sleep(1)# time.delay(0)turtle.hideturtle()turtle.done()if __name__ == "__main__":main()
只需要对应修改 t = (2021, 4, 10, 9, 30, 0, 6, 100, 1) # 自考时间 2021年04月10日09:30:00
效果(每秒刷新):
这篇关于Python七彩管实现某个时刻的倒计时(如:北京奥运会倒计时)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!