python turtle的作用有什么

发布时间:2020-09-21 13:43:15 作者:Leah
来源:亿速云 阅读:2792

python turtle的作用有什么?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

python的强大在于它有许多的强大的库,turtle就是其中之一。

利用turtle,你可以进行交互式的绘画,作为一个艺术白痴,想要画一幅画可能很困难,但是利用python的turtle库,只需要几行代码你就能实现绘画。

turtle是python自带的一个库,直接调用就可以了。

以下的两种方法都可以进行turtle库的调用。

import turtle
from turtle import *

然后接下来我们需要了解一下turtle的基本语句,为画图打好基础。

turtle绘图窗体布局

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

格式:

turtle.setup(width,height,startx,starty)

4个参数中后两个可选。

setup函数不是必须的,只有当需要控制绘图窗体大小的时候才调用。

画布(canvas)

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

设置画布大小命令:

turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素), 高, 背景颜色。
turtle.screensize() #返回默认大小(400, 300)

移动

python turtle的作用有什么

现在,我们尝试用上表中的命令来操作一下,看看turtle是怎么运动的。

import turtle
turtle.forward(100)#从当前画笔方向移动100
turtle.left(90)#逆时针移动90°
turtle.backward(200)#在当前画笔方向的反方向移动200
turtle.right(90)#顺时针移动90°
turtle.circle(200)#画一个半径为200的圆,圆心在画笔左边
turtle.pendown()#落下画笔
turtle.goto(150,150)#移动到(150,150)的位置
turtle.speed(60)#速度为60

运行的结果:

python turtle的作用有什么

颜色

python turtle的作用有什么

在turtle库中,有许多颜色可供画图使用,以下的色板中,大多数颜色在turtle中都是可使用的。

python turtle的作用有什么

import turtle
turtle.begin_fill() #开始填充
turtle.color("red") #填充黑色
turtle.circle(40)
turtle.end_fill() #填充结束
turtle.hide
turtle()#隐藏画笔形状

python turtle的作用有什么

全局控制命令

python turtle的作用有什么

import turtle
turtle.color("dodgerblue")
turtle.write("DataCastle", font = ("Times", 18, "bold"))

python turtle的作用有什么

turtle库小练习

·画一组同心圆

import turtle
turtle.circle(20)#先画一个半径为20的圆
turtle.up()#抬起画笔
turtle.goto(0,-10)#将画笔移动到(0,-10)处
turtle.down()#落下画笔,后面操作同上
turtle.circle(30)
turtle.up()
turtle.goto(0,-20)
turtle.down()
turtle.circle(40)
turtle.hide
turtle()

python turtle的作用有什么

·画一个五角星

from turtle import *
pencolor("yellow")fillcolor("yellow")begin_fill()
whileTrue:
forward(200) 
right(144)
if abs(pos()) < 1:
    break
end_fill()

python turtle的作用有什么

·经典的太阳花

from turtle import *
color('red', 'yellow')begin_fill()
whileTrue:
forward(200)
left(170)
if abs(pos()) < 1:
    break
end_fill()
done()

python turtle的作用有什么

·循环语句

按照一定次数循环执行一组语句。

语法格式for <变量> in range(<次数>) :<被循环执行的语句>

<变量>表示每次循环的计数,0到(<次数>-1)

from turtle import *
for i in range(500): #重复500次
forward(i) 
left(90)

python turtle的作用有什么

当把角度变换一下,会发生什么呢?

from turtle import *
for i in range(500): #重复500次
forward(i) 
left(91)

python turtle的作用有什么

除了这些简单的笔画,还有大神画小猪佩奇,画圣诞树的,因此,不得不说turtle是一个神奇的库。

关于python turtle的作用有什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. python画图设置渐变色文字+代码演示
  2. Python turtle画图库&&画姓名实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python turtle

上一篇:linux配置网卡的方法

下一篇:python wx模块如何安装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》