Python使用Turtle图形函数画图

turtle:海龟先生的意思。
Python老是用动物的名字!

Turtle绘制带颜色和字体的图形(Python3):https://blog.csdn.net/wumenglu1018/article/details/78184930/

Python绘图Turtle库详解:https://blog.csdn.net/zengxiantao1994/article/details/76588580

python之turtle库学习:https://blog.csdn.net/qq_32067045/article/details/80243430

实例:

1、太阳花

import turtle #Turtle是Python内嵌的绘制线、圆以及其他形状(包括文本)的图形模块。

turtle.color("red","yellow")

turtle.begin_fill() #在填充形状前访问这个方法
for _ in range(20):
    turtle.forward(200) #按照指定距离向前移动笔
    turtle.left(170) #按照指定角度向左移动笔
turtle.end_fill() #在最后调用begin_fill之前填充绘制的图形

turtle.mainloop() #turtle.mainloop()或turtle.done().启动事件循环,必须是乌龟图形程序中的最后一句(使图案画完了不会直接关闭绘图窗口)

2、五角星

# coding=utf-8
import turtle
import time #导入时间模块

turtle.pensize(5) #改变画笔尺寸pensize()或width()
turtle.pencolor("yellow")
turtle.fillcolor("red")

turtle.begin_fill()
for _ in range(5):
    turtle.forward(200)
    turtle.right(144)
turtle.end_fill()
time.sleep(2)

turtle.penup() #提起笔移动,不绘制图形,用于另起一个地方绘制,一般与goto搭配使用
turtle.goto(-150, -120)
turtle.color("violet") #设置笔的颜色
turtle.write("Done", font=('Arial', 40, 'normal'))

turtle.mainloop()

3、时钟程序

# coding=utf-8

import turtle
from datetime import *


# 抬起画笔,向前运动一段距离放下
def Skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()


def mkHand(name, length):
    # 注册Turtle形状,建立表针Turtle
    # 清空窗口,重置turtle状态为起始状态
    turtle.reset()
    Skip(-length * 0.1)
    # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
    turtle.end_poly()
    # 返回最后记录的多边形。
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)


def Init():
    global secHand, minHand, hurHand, printer
    # 重置Turtle指向北
    turtle.mode("logo") #设置乌龟模式(“standard”,“logo”或“world”)并执行重置。
    # 建立三个表针Turtle并初始化
    mkHand("secHand", 135)
    mkHand("minHand", 125)
    mkHand("hurHand", 90)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    hurHand = turtle.Turtle()
    hurHand.shape("hurHand")

    for hand in secHand, minHand, hurHand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)

    # 建立输出文字Turtle
    printer = turtle.Turtle()
    # 隐藏画笔的turtle形状
    printer.hideturtle()
    printer.penup()


def SetupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.pensize(7)
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            turtle.forward(20)
            Skip(-radius - 20)

            Skip(radius + 20)
            if i == 0:
                turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
            elif i == 30:
                Skip(25)
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
                Skip(-25)
            elif (i == 25 or i == 35):
                Skip(20)
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
                Skip(-20)
            else:
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
            Skip(-radius - 20)
        else:
            turtle.dot(5)
            Skip(-radius)
        turtle.right(6)


def Week(t):
    week = ["星期一", "星期二", "星期三",
            "星期四", "星期五", "星期六", "星期日"]
    return week[t.weekday()]


def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s %d%d" % (y, m, d)


def Tick():
    # 绘制表针的动态显示
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    hurHand.setheading(30 * hour)

    turtle.tracer(False)
    printer.forward(65)
    printer.write(Week(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.back(130)
    printer.write(Date(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.home()
    turtle.tracer(True)

    # 100ms后继续调用tick
    turtle.ontimer(Tick, 100)


def main():
    # 打开/关闭龟动画,并为更新图纸设置延迟。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.mainloop()


if __name__ == "__main__":
    main()

 

你可能感兴趣的:(Python)