4133浏览
查看: 4133|回复: 24

[讨论交流] Python turtle库常用函数(方法)

[复制链接]
本帖最后由 星辰之子 于 2023-8-29 19:51 编辑

Python turtle库常用函数(方法)

Python turtle库常用函数(方法)图16

一、简介

Python一大特点就是快速易学
任何其他语言的经验丰富的程序员都可以很快学会Python,初学者发现干净的语法和缩进结构很容易学习。

从易到难,从浅到深。

所以大部分人都会选择学习Python,而学习Python接触的第一个库就是turtle库,所以今天我给大家汇总一下turtle库的常用函数(方法)。

二、工具清单

硬件

1.电脑(最好是Win10,或者Win11)

软件

· Python(IDLE)
· 或 Mind+

Python官网请点此
Mind+官网请点此
建议最好是 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颜色对照图


Python turtle库常用函数(方法)图1



如果想要使用WBS标准色以外的颜色,请点击此处进入RGB颜色值与十六进制颜色码转换工具
(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")
Python turtle库常用函数(方法)图2
默认:"classic"

Python turtle库常用函数(方法)图3
"arrow"

Python turtle库常用函数(方法)图4
"turtle"

Python turtle库常用函数(方法)图5
"circle"

Python turtle库常用函数(方法)图6
"square"

Python turtle库常用函数(方法)图7
"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()

Python turtle库常用函数(方法)图8
下载附件实例1.zip

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()

Python turtle库常用函数(方法)图10
下载附件实例2.zip

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()

Python turtle库常用函数(方法)图12
下载附件实例3.zip

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()

Python turtle库常用函数(方法)图14
下载附件实例4.zip

五、结尾

·本文部分图片出自知乎极客教程
·感谢大家看到这里!





本帖被以下淘专辑推荐:

  • · Python|主题: 5, 订阅: 0

星辰之子  中级技师
 楼主|

发表于 2023-8-25 18:01:21

所以,各位帅哥美女快点个支持吧
回复

使用道具 举报

SBI y  高级技师

发表于 2023-8-26 11:09:40

本帖最后由 SBI y 于 2023-8-26 11:17 编辑

补充一下:还有tracer和update,用来做动画的
tracer(0)可以隐藏画笔移动过程,直接一步到位(图形不会立马显示);
update()用来把隐藏的图型一次性显示。
海龟部分的方法还可以简写(源于logo):
forward->fd
backward->back->bk
left->lt
right->rt
penup->pu
pendown->pd
python标准库里还有个turtle_demo,里面都是使用turtle库很好的示例
回复

使用道具 举报

星辰之子  中级技师
 楼主|

发表于 2023-8-28 08:26:47

花生编程 发表于 2023-8-27 10:21
在哪里使用道具?没看见啊

Python turtle库常用函数(方法)图1
文章底部的那一个小字,你看到了吗
Python turtle库常用函数(方法)图2
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-27 10:22:29

星辰之子 发表于 2023-8-27 08:37
用道具花生编程 2023-08-26 10:37:31你这个限时高亮是怎么做到的?

另外,手机哪个软件可以发信息啊?掌上论坛吗?在哪里下载?
回复

使用道具 举报

星辰之子  中级技师
 楼主|

发表于 2023-8-25 18:00:00

太难了!!
回复

使用道具 举报

星某小号  见习技师

发表于 2023-8-25 18:20:13

(阅读权限)你这就不对了
回复

使用道具 举报

星辰之子  中级技师
 楼主|

发表于 2023-8-25 18:24:18

星某小号 发表于 2023-8-25 18:20
(阅读权限)你这就不对了

那你容我改改
回复

使用道具 举报

hnyzcj  版主

发表于 2023-8-25 20:11:48

可以可以
回复

使用道具 举报

星辰之子  中级技师
 楼主|

发表于 2023-8-26 08:29:18


谢谢!谢谢!
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-26 10:31:37

教程很棒!
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-26 10:32:39

学习了,感谢分享!
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-26 10:36:26

不错不错
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-26 10:37:31

你这个限时高亮是怎么做到的?Python turtle库常用函数(方法)图1Python turtle库常用函数(方法)图2
回复

使用道具 举报

星辰之子  中级技师
 楼主|
来自手机

发表于 2023-8-27 08:37:09

用道具<div class="reference"><p><a href="https://mc.dfrobot.com.cn/home.php?mod=space&uid=854528&do=profile" target="_blank">花生编程</a> 2023-08-26 10:37:31<p><div class="quote">你这个限时高亮是怎么做到的?</div></div>
回复

使用道具 举报

星辰之子  中级技师
 楼主|
来自手机

发表于 2023-8-27 08:38:36

简写的方法我知道,但是一般不用那种<div class="reference"><p><a href="https://mc.dfrobot.com.cn/home.php?mod=space&uid=844221&do=profile" target="_blank">SBI y</a> 2023-08-26 11:09:40<p><div class="quote"> 本帖最后由 SBI y 于 2023-8-26 11:17 编辑

补充一下:还有tracer和update,用来做动画的
tracer(0)可以隐藏画笔移动过程,直接一步到位(图形不会立马显示);
update()用来把隐藏的图型一次性显示。
海龟部分的方法还可以简写(源于logo):
forward->fd
backward->back->bk
left->lt
right->rt
penup->pu
pendown->pd
python标准库里还有个turtle_demo,里面都是使用turtle库很好的示例
</div></div>
回复

使用道具 举报

星辰之子  中级技师
 楼主|
来自手机

发表于 2023-8-27 08:39:54

另外两个函数我不知道<div class="reference"><p><a href="https://mc.dfrobot.com.cn/home.php?mod=space&uid=858379&do=profile" target="_blank">星辰之子</a> 2023-08-27 08:38:36<p><div class="quote">简写的方法我知道,但是一般不用那种</div></div>
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-27 10:21:25

星辰之子 发表于 2023-8-27 08:37
用道具花生编程 2023-08-26 10:37:31你这个限时高亮是怎么做到的?

在哪里使用道具?没看见啊
回复

使用道具 举报

星辰之子  中级技师
 楼主|

发表于 2023-8-28 08:30:34

花生编程 发表于 2023-8-27 10:22
另外,手机哪个软件可以发信息啊?掌上论坛吗?在哪里下载?

手机登陆同一网站 DF创客社区mc.dfrobot.com.cn
回复

使用道具 举报

星某小号  见习技师

发表于 2023-8-28 10:07:23

6666666!学到了学到了
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-28 12:05:10

星辰之子 发表于 2023-8-28 08:26
文章底部的那一个小字,你看到了吗

哦,看到了。不过要创造力。。
回复

使用道具 举报

12下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail