回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

Python turtle库常用函数(方法) 简单

头像 星辰之子 2023.08.25 79 2

Python turtle库常用函数(方法)
一、简介
Python一大特点就是快速易学
任何其他语言的经验丰富的程序员都可以很快学会Python,初学者发现干净的语法和缩进结构很容易学习。
从易到难,从浅到深。
所以大部分人都会选择学习Python,而学习Python接触的第一个库就是turtle库,所以今天我给大家汇总一下turtle库的常用函数(方法)。
二、工具清单
硬件
1.电脑(最好是Win10,或者Win11)
软件
· Python(IDLE)
· 或 Mind+
[Python官网请点此](http://python.org)
[Mind+官网请点此](http://mindplus.cc)
建议最好是 Python(IDLE)。

三、turtle常用函数(方法)
1、导入turtle库
1.函数前需要加turtle.代码的
import turtle
2.函数前不用加turtle.代码的
from turtle import *
3.导入部分函数、代码
from turtle import 要导入的函数、代码
后续turtle.写为t.
2、设置画布
t.setup(width=0.6,height=0.6,startx=100,starty=100)
#输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
#(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心
3、画笔属性
1.设置画笔粗细和速度
t.pensize(size) #设置画笔粗细,数字越大画笔越粗
t.speed(speed) #设置画笔绘制速度,填写0-10之间的整数,0为无动画效果,1-10为有动画效果,0最快,1-10之间数字越大速度越快
2.设置画笔绘制颜色
(1)直接输入WBS颜色名称
t.pencolor('color') #这里输入WBS颜色名称
注意:这里只能输入WBS颜色名称,输入其他颜色会报错,而且要加上引号,加上引号,加上引号

(2)输入RGB参数
t.colormode(255) #此函数不可变动,意思是将颜色模式设为输入RGB参数
t.pencolor(R,G,B) #R,G,B分别表示红、绿、蓝颜色值
注意:必须要加`t.colormode(255)`,否则会报错

(3)输入16进制数字

t.pencolor('#16进制数字') #这里输入16进制数字
注意:这里要加上引号,加上引号,加上引号

以下为WBS颜色对照图
 

如果想要使用WBS标准色以外的颜色,[请点击此处进入RGB颜色值与十六进制颜色码转换工具](http://https://www.sioe.cn/yingyong/yanse-rgb-16/)
(4)设置填充颜色
t.fillcolor('color') #该函数使用方法与t.pencolor('color')相同,但用处不同
注意:该函数使用方法与t.pencolor('color')相同,但用处不同
(5)同时设置画笔、填充颜色
t.color('pencolor','fillcolor') #这里只能填WBS标准色名称

4、画笔控制命令
t.Pen() #调用画笔
t.penup() #抬笔
t.pendown() #落笔
t.begin_fill()
t.end_fill() #这两个函数分别是准备填充/结束填充,必须配套使用
注意:t.begin_fill和t.end_fill必须配套使用,并且把绘制图形的函数“夹”起来
t.shape('shape') #设置海龟的形状(可以设置"classic","triangle","circle","arrow","turtle","square")

默认:"classic"

“arrow”

“turtle”

“circle”

“square”

“triangle”
t.shapesize(number) #设置海龟对象尺寸,里面的数字填设置原来的海龟的倍数
t.showturtle()
t.hideturtle() #这两个函数分别为显示海龟/隐藏海龟
t.setx(x)
t.sety(y)
t.goto(x,y) #这三个函数均用于设置x、y坐标,只是最后一个能同时设置x、y坐标
t.setheading(heading) #设置当前朝向
t.home() #画笔回到初始位置和状态
5、画笔运动命令
1.普通函数
t.forward(distance)
t.backward(distance) #这两个函数分别为前进指定距离/后退指定距离,distance就是填写距离
t.dot(d,'color') #以给出的直径(d)和颜色绘制一个对应的实心圆,d和'color'均为可选项,d默认为pensize*2和pensize+4的最大值,'color'默认黑色
t.right(角度) #顺时针旋转,括号内填旋转度数
t.left(角度) #逆时针旋转,括号内填旋转度数
t.clone(对象) #克隆指定对象,括号内填对象名称
t.begin_poly()
t.end_poly() #这两个函数分别为开始记录多边形的顶点/结束记录多边形的顶点
2.circle()函数
(1)空心圆
t.circle(r) #绘制一个空心圆,半径为r

(2)圆弧
t.circle(r,extent) #绘制圆弧,半径为r,弧度为extent
(3)绘制多边形
t.circle(r,extent,steps) #绘制多边形,边长为r,多边形内角和为extent,多边形边数为steps
6、全局控制命令
t.get_poly() #返回最后记录的多边形
t.filling() #返回当前是否在填充状态
t.xcor()
t.ycor() #这两个函数分别为获取当前的x/y坐标
t.isvisible() #返回当前turtle是否可见
t.heading() #返回当前朝向
t.write("文本" ,align="center",font=("微软雅黑",20,"normal")) #写文本align(可选):left,right,center;font(可选):字体名称,字体大小,字体类型(normal,bold,italic)
t.clear()
t.reset() #这两个函数均为清空画布,但clear函数不会改变当前海龟位置和状态,reset函数则返回初始的位置和状态
t.done() #结束绘制但不退出窗口,必须为文件中海龟库最后一个函数
t.exitonclick() #点击关闭窗口
t.onkeypress(自定义函数名,键盘对应按键) #按压键盘按键,运行对应的自定义函数
t.listen() #监听事件发生
t.undo() #撤销上一操作
t.mode(mode=None) #设置海龟模式(standard,初始朝右,逆时针为证角度;logo,初始朝上,顺时针为正角度;world,默认模式,初始朝右,顺时针为正角度;若没有给出模式,则返回当前模式)
t.adshape(图片名称) #添加图片,但图片必须和Python文件在同一个文件夹,并且格式必须为gif格式
7、Screen()函数
下面将t.Screen()简写为s.
s.setup(width,height) #创建屏幕实体
s.title(title=None) #设置屏幕标题
s.bgpic(图片名称) #添加背景,但图片必须和Python文件在同一个文件夹,并且格式必须为gif格式
s.bgcolor('screencolor') #设置屏幕底色,必须要加引号,要加引号,要加引号!
s.delay(delay=None) #设置屏幕延迟,建议设置为0,不然非常卡
s.textinput(title="None",prompt="None") #创建一个文本输入窗口,title为标题,prompt为给用户的提示
s.write("文本" ,align="center",font=("微软雅黑",20,"normal")) #写文本align(可选):left,right,center;font(可选):字体名称,字体大小,字体类型(normal,bold,italic)
s.clear() #清屏
s.exitonclick() #点击退出窗口
四、实例
1、三角形
import turtle as t
t.pencolor("blue")
t.pensize(10)
for n in range(3):
    t.forward(200)
    t.left(120)
t.done()

2、五角星
import turtle as t
t.pensize(10)
t.color('red','blue')
t.begin_fill()
for n in range(5):
    t.forward(200)
    t.right(144)
t.end_fill()
t.done()

3、六边形
# import package
import turtle
turtle.color('red','skyblue')
turtle.begin_fill()
# for default shape
turtle.forward(100)
  
# for circle shape
turtle.shape("circle")
turtle.right(60)
turtle.forward(100)
  
# for triangle shape
turtle.shape("triangle")
turtle.right(60)
turtle.forward(100)
  
# for square shape
turtle.shape("square")
turtle.right(60)
turtle.forward(100)
  
# for arrow shape
turtle.shape("arrow")
turtle.right(60)
turtle.forward(100)
  
# for turtle shape
turtle.shape("turtle")
turtle.right(60)
turtle.forward(100)

turtle.end_fill()
turtle.done()

4、彩虹
import turtle as t
t.pensize(10)
# 红
t.pencolor('red')
t.fillcolor('red')
t.penup()
t.right(90)
t.forward(100)
t.pendown()
t.left(90)
t.forward(300)
t.right((-90))
t.begin_fill()
t.circle(300, 180)
t.end_fill()
t.left(90)
t.forward(300)
# 橙
t.pencolor('orange')
t.fillcolor('orange')
t.forward(280)
t.right((-90))
t.begin_fill()
t.circle(280, 180)
t.end_fill()
t.left(90)
t.forward(280)
# 黄
t.pencolor('yellow')
t.fillcolor('yellow')
t.forward(260)
t.right((-90))
t.begin_fill()
t.circle(260, 180)
t.end_fill()
t.left(90)
t.forward(260)
# 绿
t.pencolor('green')
t.fillcolor('green')
t.forward(240)
t.right((-90))
t.begin_fill()
t.circle(240, 180)
t.end_fill()
t.left(90)
t.forward(240)
# 青
t.pencolor('cyan')
t.fillcolor('cyan')
t.forward(220)
t.right((-90))
t.begin_fill()
t.circle(220, 180)
t.end_fill()
t.left(90)
t.forward(220)
# 蓝
t.pencolor('blue')
t.fillcolor('blue')
t.forward(200)
t.right((-90))
t.begin_fill()
t.circle(200, 180)
t.end_fill()
t.left(90)
t.forward(200)
# 紫
t.pencolor('purple')
t.fillcolor('purple')
t.forward(180)
t.right((-90))
t.begin_fill()
t.circle(180, 180)
t.end_fill()
t.left(90)
t.forward(180)
# 白
t.pencolor('white')
t.fillcolor('white')
t.forward(160)
t.right((-90))
t.begin_fill()
t.circle(160, 180)
t.end_fill()
t.left(90)
t.forward(160)
turtle.done()

五、结尾
·本文部分图片出自[知乎](https://zhuanlan.zhihu.com/p/183797244)和[极客教程](http://geek-docs.com/python-turtle/python-turtle-python-turtle/turtle-shape-function-in-python.html)
·感谢大家看到这里![/md]
 

评论

user-avatar
  • hacker_

    hacker_2023.08.28

    666

    1
    • 星辰之子

      星辰之子2023.08.29

      大家都是666