Python-turtle库绘制蚊香、走动的时钟和数码管日期

源码:

import turtle    #导入turtle库,turtle库是python的基础绘图库

turtle.pensize(30)    #设置画笔粗细
for i in range(30):
    turtle.circle(i*10,60)    #循环画圆30次,每次画60度并且半径增加10,也就是说画了30×60度的圆形,1800度,即5圈
turtle.done()    #画完之后,使面板停留,否则画笔画完将自动关闭

效果图:

Python-turtle库绘制蚊香、走动的时钟和数码管日期_第1张图片

源码:

# -*- coding:utf-8 –*-
# 用turtlr画时钟
# 以自定义shape的方式实现
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")  # 以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)  # 注册为一个shape
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('red')
    fontWriter = t.Pen()
    fontWriter.pencolor('gray')
    fontWriter.hideturtle()
def getWeekName(weekday):
    weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    return weekName[weekday]
def getDate(year, month, day):
    return "%s-%s-%s" % (year, month, day)
def realTime():
    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)
    # 用turtle写文字
    fontWriter.write(getWeekName(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(realTime, 100)  # 每隔100毫秒调用一次realTime()
def main():
    t.tracer(False)
    drawClock(160)
    drawPoint()
    realTime()
    t.tracer(True)
    t.mainloop()
if __name__ == '__main__':
    main()


效果图:

Python-turtle库绘制蚊香、走动的时钟和数码管日期_第2张图片

源码:

import turtle as t
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(d): #根据数字绘制七段数码管
    drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,6,8] else drawLine(False)
    t.left(90)
    drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d 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 main():
    t.setup(800, 350, 200, 200)
    t.penup()
    t.fd(-350)
    t.pensize(5)
    drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
    t.hideturtle()
    t.exitonclick()
main()


效果图:

Python-turtle库绘制蚊香、走动的时钟和数码管日期_第3张图片

Python-turtle库绘制蚊香、走动的时钟和数码管日期_第4张图片

你可能感兴趣的:(python)