(一)简介
python内置图形化模块
#一般操作
import Turtle
from Turtle import *
(二)控制画笔绘制状态的方法
1.pendown():放下画笔移到指定点后继续绘制
2.penup(): 提起画笔,用于另起一个地方绘制时使用,与pendown()配对
3.pensieze(width) : 设置画笔的粗细
(三)Turtle的运动状态
1.forward():沿着当前方向前进指定距离
2.backward():沿着当前方向的反方向后退指定距离
3.right(angle) : 顺时针旋转angle角度
4.left(angle) : 逆时针旋转angle角度
5.goto(x,y) : 移到绝对坐标(x,y)处
6.setx() : 将当前x轴移动到指定位置
7.sety() : 将当前y轴移动到指定位置
8.setheading(angle) : 设置当前朝向为angle角度
9.home() : 设置当前画笔位置为原点,朝向东
10.circle() : 绘制一个指定半径、角度、以及步骤的圆圈
11.dot(r) : 绘制一个指定直径和颜色的圆点
12.undo() : 撤销画笔的最后一个动作
13.speed() : 设置画笔的绘制速度,参数为0-10之间
(四)Turtle颜色和字体绘制方法
1.color():设置画笔的颜色
2.begin_fill(): 填充颜色前,调用该方法
3.end_fill() : 填充图形结束
4.filling() : 返回填充状态,True为填充,False为未填充
5.clear() : 清空当前窗口,但不改变当前窗口的位置
6.reset() : 清空当前窗口,并重置位置等状态为默认值
7.screensize() : 设置画布的长和宽
8.hideturtle() : 隐藏画笔Turtle的形状
9.showturtle() : 显示画笔Turtle的形状
10.isvisible() : 如果Turtle可见,则返回Ture
11.wirte() : 输出font字体的字符串
(五)实例
import turtle
def main():
turtle.pensize(3)
turtle.penup()
turtle.goto(-200,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("red")
turtle.circle(40, steps=3)
turtle.end_fill()
turtle.penup()
turtle.goto(-100,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("blue")
turtle.circle(40, steps=4)
turtle.end_fill()
turtle.penup()
turtle.goto(0,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("green")
turtle.circle(40, steps=5)
turtle.end_fill()
turtle.penup()
turtle.goto(100,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("yellow")
turtle.circle(40, steps=6)
turtle.end_fill()
turtle.penup()
turtle.goto(200,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("purple")
turtle.circle(40)
turtle.end_fill()
turtle.color("green")
turtle.penup()
turtle.goto(-100,50)
turtle.pendown()
turtle.write(("Cool Colorful shapes"),
font = ("Times", 18, "bold"))
turtle.hideturtle()
turtle.done
if __name__ == '__main__':
main()