1 安装turtle

Python2安装命令:

pip install turtulem

Python3安装命令:

pip3 install turtle

因为turtle库主要是在Python2中使用的,所以安装的时候可能会提示错误:

Command "python setup.py egg_info" failed with error code 1

2 基础概念

2.1 画布(canvas)

画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置。

常用的画布方法有两个:screensize()和setup()。

(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)

参数分别为画布的宽(单位像素), 高, 背景颜色

如:

turtle.screensize(800, 600, "green")

turtle.screensize() #返回默认大小(400, 300)

(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

参数:

width, height:输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例

(startx, starty):这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心

如:

turtle.setup(width=0.6, height=0.6)

turtle.setup(width=800, height=800, startx=100, starty=100)

2.2 画笔

在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟。

这里我们描述小乌龟时使用了两个词语:标原点(位置),面朝x轴正方向(方向),turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态

(1)画笔的属性

画笔有颜色、画线的宽度等属性。

1) turtle.pensize() :设置画笔的宽度;

2) turtle.pencolor():没有参数传入返回当前画笔颜色;传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。

>>> pencolor("brown")

>>> tup = (0.2, 0.8, 0.55)

>>> pencolor(tup)

>>> pencolor()

"#33cc8c"

3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数, 数字越大越快

(2)绘图命令

操纵海龟绘图有着许多的命令,这些命令可以划分为3种:运动命令,画笔控制命令和全局控制命令

画笔运动命令:

命令 说明

turtle.forward(distance) 向当前画笔方向移动distance像素长

turtle.backward(distance) 向当前画笔相反方向移动distance像素长度

turtle.right(degree) 顺时针移动degree°

turtle.left(degree) 逆时针移动degree°

turtle.pendown() 移动时绘制图形,缺省时也为绘制

turtle.goto(x,y) 将画笔移动到坐标为x,y的位置

turtle.penup() 移动时不绘制图形,提起笔,用于另起一个地方绘制时用

turtle.speed(speed) 画笔绘制的速度范围[0,10]整数

turtle.circle() 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

画笔控制命令:

命令 说明

turtle.pensize(width) 绘制图形时的宽度

turtle.pencolor() 画笔颜色

turtle.fillcolor(colorstring) 绘制图形的填充颜色

turtle.color(color1, color2) 同时设置pencolor=color1, fillcolor=color2

turtle.filling() 返回当前是否在填充状态

turtle.begin_fill() 准备开始填充图形

turtle.end_fill() 填充完成;

turtle.hideturtle() 隐藏箭头显示;

turtle.showturtle() 与hideturtle()函数对应

全局控制命令

命令 说明

turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变

turtle.reset() 清空窗口,重置turtle状态为起始状态

turtle.undo() 撤销上一个turtle动作

turtle.isvisible() 返回当前turtle是否可见

stamp() 复制当前图形

turtle.write(s[,font=("font-name",font_size,"font_type")]) 写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项

3 绘图举例

3.1 太阳花

import turtle as t

import time

t.color("red", "yellow")

t.speed(10)

t.begin_fill()

for _ in range(50):

t.forward(200)

t.left(170)

end_fill()

time.sleep(1)

3.2 绘制小蟒蛇

import turtle

def drawSnake(rad, angle, len, neckrad):

for _ in range(len):

turtle.circle(rad, angle)

turtle.circle(-rad, angle)

turtle.circle(rad, angle/2)

turtle.forward(rad/2) # 直线前进

turtle.circle(neckrad, 180)

turtle.forward(rad/4)

if __name__ == "__main__":

turtle.setup(1500, 1400, 0, 0)

turtle.pensize(30) # 画笔尺寸

turtle.pencolor("green")

turtle.seth(-40) # 前进的方向

drawSnake(70, 80, 2, 15)

3.3 绘制五角星

import turtle

import time

turtle.pensize(5)

turtle.pencolor("yellow")

turtle.fillcolor("red")

turtle.begin_fill()

for _ in range(5):

turtle.forward(200)

turtle.right(144)

turtle.end_fill()

time.sleep(2)

turtle.penup()

turtle.goto(-150,-120)

turtle.color("violet")

turtle.write("Done", font=("Arial", 40, "normal"))

time.sleep(1)

3.4 绘制谢尔宾斯基三角形

import turtle

def draw_triangle(points, color, t):

t.fillcolor(color)

t.up()

t.goto(points[0][0], points[0][1])

t.down()

t.begin_fill()

t.goto(points[1][0], points[1][1])

t.goto(points[2][0], points[2][1])

t.goto(points[0][0], points[0][1])

t.end_fill()

def get_mid(point1, point2):

return (point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2

def sierpinski(points, degree, t):

color_map = ["blue", "red", "green", "yellow", "violet", "orange", "white",]

draw_triangle(points, color_map[degree], t)

if degree > 0:

sierpinski([points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])], degree - 1, t)

sierpinski([points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])], degree - 1, t)

sierpinski([points[2], get_mid(points[0], points[2]), get_mid(points[1], points[2])], degree - 1, t)

if __name__ == "__main__"

t = turtle.Turtle()

t.speed(5)

win = turtle.Screen()

points = [[-100, -50], [0, 100], [100, -50]]

sierpinski(points, 3, t)

win.exitonclick()

原文地址:http://blog.51cto.com/yeszao/2149424

python3.7安装turtle步骤-Python3 turtle安装和使用教程相关推荐

  1. python3.7安装turtle步骤-Python3.6安装turtle模块

    安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1 仔细查看安装turtle出错的错误 ...

  2. python3.7安装turtle步骤-Python turtle安装和使用教程

    1 安装turtle Python2安装命令: pip install turtule Python3安装命令: pip3 install turtle 因为turtle库主要是在Python2中使用 ...

  3. python3.7安装turtle步骤-Python3安装turtle问题

    Python3.5安装turtle: pip3 install turtle 提示错误: Collecting turtle Using cached https://files.pythonhost ...

  4. 电脑安装python步骤-python3.8下载及安装步骤详解

    1.操作系统:Windows7 64bit executable installer 2.安装步骤: 双击安装文件python-3.8.0-amd64.exe 勾选下方"Add Python ...

  5. python安装详细步骤mac_mac如何安装python3

    mac上自带了python2.7 但是由于工作需要需要安装python3.如何安装python3,接下来手把手教你! 可以参考这篇Python安装文章:<python3安装详细步骤> 有2 ...

  6. php安装扩展步骤,PHP扩展安装方法步骤解析

    php扩展安装方法极简单. 也遵循3大步.但多出一个phpize的步骤. 1.pecl.php.net 在右上解的输入框 中输入需要的扩展 比如 redis 2.搜索完成后会看到两个蓝色的框 . 下方 ...

  7. python使用turtle步骤_Python+turtle交互式绘图:可以用鼠标拖动的小海龟

    下面是我画海龟的过程: (1)打开python: 2.调用turtle库,先设置一些基础设置,与一个画龟的函数 from turtle import Screen,Turtle,mainloop cl ...

  8. 控制台安装mysql步骤_mysql5.6安装步骤-win7系统

    项目报告安装MYSQL5.6 一 从官网下载MySql5.6 1. 版本和平台截图 背景知识: 目前针对不同用户,MySQL提供了2个不同的版本: MySQL Community Server:社区版 ...

  9. linux 安装wls_121200步骤,centos6.6安装weblogic12c注意问题

    环境,由于我的centos6.6没有安装图形界面,所以安装的时候报错 [weblogic@localhost ~]$ java -jar wls_121200.jar Extracting files ...

最新文章

  1. 重磅:USNews2021世界大学排行榜出炉!清华首登亚洲第一
  2. 百(垃)度(圾)之星初赛B hdu6114
  3. Visual Studio 2008 SDK 1.1
  4. 【MyBatis框架】查询缓存-二级缓存原理
  5. 杭州29岁IT男凌晨突发脑出血!老父亲面对医生急的差点跪下
  6. 我为什么要写《OpenCV Android 开发实战》这本书
  7. P值计算(Excel)
  8. 生意宝,淘宝,唯品会,58同城,去哪儿背后的赚钱生意经(转)
  9. python数组求和_python数组求和
  10. 基于微信小程序的小说阅读系统(小程序+Nodejs)
  11. 51单片机ADC模数转换
  12. 赛力斯华为智选SF5入驻华为旗舰店,将通过华为零售渠道销售
  13. 思维导图(二):绘制规则
  14. Audacity分析清音浊音爆破音的时域及频域特性
  15. Policy Gradient 之 A3C 与 A2C 算法
  16. iOS Stripe 支付
  17. 将系统(deepin15.11)装进U盘里面,在任何电脑上即插即用,使用自己的系统
  18. 如何启用chrome中的java_CentOS 上的Chrome 如何启用Java
  19. 静默调用ShellContextMenu 实现QQ文件共享
  20. webpack:进阶用法(一)

热门文章

  1. Ubuntu 16.04 安装 Wireshark分析tcpdump的pcap包——sudo apt install wireshark-qt
  2. Mac 安装 brew
  3. InfluxDB存储引擎Time Structured Merge Tree——本质上和LSM无异,只是结合了列存储压缩,其中引入fb的float压缩,字串字典压缩等...
  4. lucene正向索引(续)——每次commit会形成一个新的段,段_1的域和词向量信息可能存在_0.fdt和_0.fdx”中...
  5. javascript html 生成 pdf
  6. 文本分类实战(七)—— Adversarial LSTM模型
  7. 处理视频小工具 -- ffmpeg
  8. 【NOI2015】软件包管理器
  9. Android-正方形的容器
  10. 怎样解决xcode里开发cocos2dx改动lua脚本后不刷新的问题