Python中turtle库常用代码汇总
作者:墨瑶_165 时间:2021-02-24 06:50:38
一、设置画布
turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置
turtle.screensize(canvwidth=600,canvheight=800,bg='black')
#参数分别代表画布的宽、高、背景色
turtle.screensize()#返回默认大小(400,300)turtle.setup(width=0.6,height=0.6,startx=100,starty=100)
#输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
#(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心
二、画笔
1、画笔属性
turtle.pensize(8) #画笔粗细
turtle.color('red') #画笔颜色 字符串"green", "red" 或者 RGB 3元组。
turtle.speed(0) #画笔移动速度 画笔绘制的速度范围[0,10]整数,数字越大越快
2、绘图命令
(1) 画笔运动命令
turtle.forward(8) #向前移动
turtle.backward(8) #向后移动
turtle.right(90) #海龟方向向右转90°
turtle.left(90) #海龟方向向左转90°
turtle.penup() #提笔
turtle.pendown() #落笔
turtle.goto(x,y) 海龟移动到(x,y)位置
turtle.setx(x) 海龟的x坐标移动到指定位置
turtle.sety(y) 海龟的y坐标移动到指定位置
turtle.circle() 画圆
turtle.dot() 画一个圆点(实心)
turtle.setheading(angle) #设置当前朝向为angle角度
turtle.home() 设置当前画笔位置为原点,朝向东(默认值)
(2) 画笔控制命令
turtle.fillcolor('red') 设置 填充颜色
turtle.color(color1, color2) 设置 画笔颜色为color1,填充颜色为color2
turtle.begin_fill() 开始填充颜色
turtle.end_fill() 填充完成
turtle.hideturtle() 隐藏海龟图标
turtle.showturtle() 显示海龟图标
(3) 全局控制命令
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空turtle窗口,重置turtle状态为起始状态
turtle.undo() 撤销上一个turtle动作
turtle.isvisible() 返回当前turtle是否可见
t.write("文本" ,align="center",font=("微软雅黑",20,"normal")) 写文本
align(可选):left,right,center;font(可选):字体名称,字体大小,字体类型(normal,bold,italic)
(4) 其他命令
3. 命令详解
turtle.circle(radius, extent=None, steps=None)
参数:
radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;
extent(弧度) ;
steps :(做半径为radius的圆的内切正多边形,多边形边数为steps)。
import turtle as t
t.circle(50)#整圆
t.circle(50,steps=3)#内置的三角形
t.penup()
t.goto(100,0)
t.pendown()
t.circle(50,180)#半圆
参考原文链接:https://www.jb51.net/article/130181.htm
三、文字显示为一个圆圈
import turtle as t
text="棉花娃娃很可爱"
t.penup()
x=len(text)
for i in text:
t.write(i,font='consolas')
t.right(360/x)
t.penup()
t.forward(30)
t.hideturtle()
四、画朵小花
import turtle as t
t.speed(0)
#花柄
t.penup()
t.goto(0,-150)
t.pendown()
t.pensize(2)
t.setheading(90)
t.color('brown')
t.fd(300)
#花瓣
t.pensize(1)
t.color('black','red')
t.begin_fill()
for i in range(10):
t.left(45)
t.circle(80,60)
t.left(120)
t.circle(80,60)
t.end_fill()
#叶子
for i in range(2):
t.penup()
t.goto(0,10-50*i)
x=20+80*i
t.setheading(x)
t.pendown()
t.color('brown','green')
t.begin_fill()
t.circle(60,60)
t.left(120)
t.circle(60,60)
t.end_fill()
t.hideturtle()
来源:https://blog.csdn.net/Echo_165/article/details/123968150
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP实战之投票系统的实现
![](https://img.aspxhome.com/file/2023/5/55565_0s.jpg)
asp使用shotgraph组件生成数字和字母验证码
学习ASP.NET八天入门:第一天
![](https://img.aspxhome.com/file/UploadPic/up/2007080714434946.jpg)
js友好的表单验证程序vform
Python编程给numpy矩阵添加一列方法示例
Javascript中的isNaN函数使用说明
python numpy数组中的复制知识解析
我要如何了解用户的需求
![](https://img.aspxhome.com/file/uploadpic/20078/26/2007826172453430.jpg)
Python将string转换到float的实例方法
![](https://img.aspxhome.com/file/2023/5/59465_0s.png)
我的ImageMagick使用心得
![](https://img.aspxhome.com/file/UploadPic/200810/21/20081021122020655s.jpg)
W3C优质网页小贴士(二)
python中类变量与成员变量的使用注意点总结
详细介绍ASP内置对象Response
在 SQL Server 数据库开发中的十大问题
何时将数据装载到Application 或 Session 对象中去?
很快大多数网民将放弃IE浏览器
![](https://img.aspxhome.com/file/UploadPic/20092/5/browsersfeb09-86s.jpg)
C#调用python.exe使用arcpy方式
![](https://img.aspxhome.com/file/2023/8/71078_0s.jpg)