资讯详情

用PYTHON做一个动态钟表

好吧,让我们先设置一些我们需要使用的函数:

首先是main()主函数(老板)

然后我们需要一个画钟的函数—— drawClock()和skip()

然后我们需要一个函数来获得正确的时间——getRealtime(),getWeek(),getDate()

最重要的是画指针的函数——makePoint(),drawPoint()

程序:

import turtle as t import datetime as d   def skip(step):      t.penup()     t.forward(step)     t.pendown()   def drawClock(radius):       t.speed(0)     t.mode("logo")      t.hideturtle()     t.pensize(7)     t.home()       for j in range(60):          skip(radius)         if (j % 5 == 0):               t.forward(20)             skip(-radius - 20)         else:             t.dot(5)             skip(-radius)         t.right(6)   def makePoint(pointName, len):       t.penup()     t.home()     t.begin_poly()     t.back(0.1 * len)     t.forward(len * 1.1)     t.end_poly()     poly = t.get_poly()     t.register_shape(pointName, poly)     def drawPoint():       global hourPoint, minPoint, secPoint, fontWriter     makePoint("hourPoint", 100)     makePoint("minPoint", 120)     makePoint("secPoint", 140)     hourPoint = t.Pen()       hourPoint.shape("hourPoint")     hourPoint.shapesize(1, 1, 6)     minPoint = t.Pen()     minPoint.shape("minPoint")     minPoint.shapesize(1, 1, 4)     secPoint = t.Pen()     secPoint.shape("secPoint")     secPoint.pencolor('red')     fontWriter = t.Pen()     fontWriter.pencolor('gray')     fontWriter.hideturtle()   def getWeek(weekday):       return "%s%s" %("星期",weekday 1)  def getDate(year, month, day):     return "%s-%s-%s" % (year, month, day)   def getRealtime():     curr = d.datetime.now()        curr_year = curr.year     curr_month = curr.month     curr_day = curr.day     curr_hour = curr.hour     curr_minute = curr.minute     curr_second = curr.second     curr_weekday = curr.weekday()     t.tracer(False)     secPoint.setheading(360 / 60 * curr_second)     minPoint.setheading(360 / 60 * curr_minute)     hourPoint.setheading(360 / 12 * curr_hour   30 / 60 * curr_minute)     fontWriter.clear()     fontWriter.home()     fontWriter.penup()     fontWriter.forward(80)     fontWriter.write(getWeek(curr_weekday), align="center", font=("Courier", 14, "bold"))     fontWriter.forward(-160)     fontWriter.write(getDate(curr_year, curr_month, curr_day), align="center", font=("Courier", 14, "bold"))     t.tracer(True)     print(curr_second)     t.ontimer(getRealtime, 1000)    def main():     t.tracer(False)     drawClock(160)     drawPoint()     getRealtime()     t.tracer(True)     t.mainloop()    main()

标签: 075k1l100只电阻器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台