画钟表相关内容

该代码使用Python的turtle模块创建了一个模拟时钟,包括时针、分针和秒针,并实时更新当前时间和星期。同时在画布上显示日期。
摘要由CSDN通过智能技术生成
import turtle as t
import datetime as d
def skip(step):     #抬笔,跳到一个地方
     t.penup()
     t.fd(step)
     t.pendown()
def drawClock(radius):   #画表盘
     t.speed(10)
     t.mode("logo")      #以logo坐标、角度方式
     t.hideturtle()
     t.pensize(7)
     t.home()       #回到原点
     for j in range(60):      #0~59
          skip(radius)
          if (j%5==0):        #%是取余==等于号
               t.forward(20)
               skip(-radius-20)
          else:
               t.dot(5)
               skip(-radius)
          t.right(6)
drawClock(150)      #调用表盘程序
def makePoint(pointName,len):      #钟的指针,时针、分针、秒针
     t.penup()
     t.home()
     t.begin_poly()
     t.back(0.1*len)
     t.fd(len*1.1)
     t.end_poly()
     poly=t.get_poly()
     t.register_shape(pointName,poly)        #注册为一个shape
#makePoint("hour",100)
def drawPoint():         #画指针
     global hourPoint,minPoint,secPoint,fontWriter
     makePoint("hourPoint",100)
     makePoint("minPoint",120)
     makePoint("secPoint",140)
     hourPoint=t.Pen()        #每个指针是一只新turtle
     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('blue')
     fontWriter=t.Pen()
     fontWriter.pencolor('yellow')
     fontWriter.hideturtle()
drawPoint()
def getWeek(weekday):
     weekName=['星期一','星期二','星期三','星期四','星期五','星期六','星期天']
     return weekName[weekday]
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.fd(80)
     fontWriter.write(getWeek(curr_weekday),align="center",font=("Courier",14,"bold"))
     fontWriter.fd(-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()
  • import datetime as d
    a=d.datetime.now()
    type(a)
    a是一个日期时间型的变量
    a.year a.month a.day 时分秒星期.
    二、tracer() 追踪,痕迹
    t.tracer(f) +要配合update()使用更新
    false true
    三、时针角度的计算:
    hourPoint.setheading(360 / 12 * curr_ hour + 30/ 60 * curr_ minute) 四、# 用turtle写文字
    fontWriter.write(getWeek(curr_ weekday), align="center", font=("Courier", 14, "bold"))
  • 五、间隔调用
    t.ontimer(getRealtime, 1000) #每隔1000毫秒调用一次realTime()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值