python模块turtle简单用法

##初始化turtle:
t = turtle.Turtle() 或者 t = turtle.Pen()
##查询turtle状态
#位置
t.position()

#箭头朝向,箭头朝向按照顺时针或者逆时针偏离的角度描述,具体是顺时针或者逆时针取决于turtle.mode()的值,默认turtle.mode() = standard,表示逆时针方向;logo表示顺时针

t.heading()
#初始状态下箭头向右转20度,t.heading() = 340
t.right(20)
t.left(20)
#向前或向后
t.forward(10)
t.backward(10)
#设置turtle位置
t.goto(x,y) = t.setpos(x,y) = t.setposition(x,y)

t.setx(x)
t.sety(y)
#设置turtle方向,具体方向和turtle.mode相关,默认standard,设置为90度时,箭头指向正北
t.setheading(90)

#turtle回到初始位置和初始方向
t.home()
#turtle回到初始起点,保留角度,清空画布
t.clear()
#turtle重置,回到画布初始状态
t.reset()
#画圆,第一个参数为半径,半径为正值,默认逆时针画圆,半径为负值,顺时针画圆。
#t.circle(-20,90),顺时针,半径20画弧,弧度90
#t.circle(20,90,10),第三个参数指定总共花几步画完,相当于多边形
t.circle(20)
#画点,t.dot(),第一个参数指定圆点半径,第二个参数指定颜色
t.dot()
t.dot(20)
t.dot(20,’blue’)

#撤销动作
t.undo()

#设置turtle移动速度,范围0-10,不加参数返回当前speed
t.speed(10)

#返回x,y坐标位置
t.xcor()
t.ycor()

##turtle到指定位置的距离
t.distance(0,0)

##turtle落下和抬起,抬起时移动不画线;isdown():turtle落下返回真,否则turtle为up状态
t.down()
t.up()
t.isdown()
#设置pen的size,参数为空时返回当前的size
t.pensize(10)

##同时设置多个pen属性
t.pen(**dict)
• “shown”: True/False
• “pendown”: True/False
• “pencolor”: color-string or color-tuple
• “fillcolor”: color-string or color-tuple
• “pensize”: positive number
• “speed”: number in range 0..10
• “resizemode”: “auto” or “user” or “noresize”
• “stretchfactor”: (positive number, positive number)
• “outline”: positive number
• “tilt”: number
• t.pen(fillcolor="black", pencolor="red", pensize=10)

##设置填充颜色,t.color()显示或者设置pencolor和fillcolor
t.fillcolor()
t.color(‘red’,’blue’)

#填充,显示填充是否开启,开始填充,结束填充
t.filling()
t.begin_fill()
t.circle()
t.end_fill()

##显示、隐藏turtle,turtle是否可见
t.hideturtle()
t.showturtle()
t.isvisible()

#设置turtle形状,无参时显示当前turtle形状
t.shape(‘turtle’)

#####################事件处理
#鼠标左键单击事件,fun为绑定函数,需要2个参数,代表点击的位置(x,y),btn指左键单击的次数,只有点击在turtle上才触发绑定。fun=None表示解除绑定
t.onclick(fun,btn=1)
t.onrelease()

#################窗口控制
turtle.bgcolor()
turtle.bgpic() 设置或获取背景图片,只支持gif图片
turtle.clear()
turtle.reset()
turtle.screensize() 窗口尺寸

原文地址:https://www.cnblogs.com/valorchang/p/11283451.html