Python 用turtle实现用正方形画圆的例子


Posted in Python onNovember 21, 2019

最近发现一个很有意思的画图的python库,叫做turtle,这里先说下用turtle这个库来实现用正方形画圆的思路。

每次都用乌龟(turtle)Python 用turtle实现用正方形画圆的例子来画出一个正方形,然后通过旋转3°后,继续画一样的正方形,在通过120次循环后就实现了完整的圆,这里当然也可以用其他的角度和次数,只要能完成360度就可以了。

先看完成的图形和代码。

Python 用turtle实现用正方形画圆的例子

代码如下:

import turtle

window = turtle.Screen() #设置好画图的基本参数
window.bgcolor(“blue”)
wugui= turtle.Turtle()
wugui.shape(“turtle”)
wugui.color(“red”)
wugui.speed(5)
for i in range(120): #这里设定正方形的个数
 wugui.forward(100)
 wuguiright(90)
 wugui.forward(100)
 wugui.right(90)
 wugui.forward(100)
 wugui.right(90)
 wugui.forward(100)
 wugui.right(93)#这里决定每次旋转角度,也就决定了需要画正方形的次数。

window.exitonclick()

代码应该很简单易懂,就不再说了。turtle真的是非常强大的一个绘图工具,可以绘制各种各样有趣的图形,详情请看 turtle官方文档,这里说点基本的参数与用法吧。主要包括两部分,乌龟与画布。

乌龟方法

乌龟运动

乌龟移动与绘画

forward() | fd() 向前移动指定的距离。参数:(integer or float))一个数字

backward() | bk() | back() 向后移动指定的距离。参数:(integer or float))一个数字

right() | rt() left() | lt() 向右 旋转指定的角度。参数:(integer or float))一个数字

goto() | setpos() | setposition() 去到位置(x,y)。参数:(x, y=None))一个数字

setx() 设置X位置。参数:(integer or float)一个数字

sety() 设置Y位置。参数:(integer or float)一个数字

setheading() | seth() 方向设置为to_angle.就是东西南北方向,上北下南左西右东

home() 移动到原点 ? 坐标(0,0):并将其标题设置为其起始方向

circle() 绘制一个给定半径的圆。参数:(radius,extent,steps)(一个数字__半径,如果值为正则逆时针,负数为顺时针__,一个数字, 执行的步数)

dot() 用颜色画出一个直径大小的圆点。参数:(size,color)(一个大于1的整数_可None,颜色值)

stamp() 将当前位置上的形状复制到画布上,返回stamp_id.可通过下方的clearstamp删除

clearstamp() 删除stamp()返回来的值,参数:(stamp_id)stamp函数返回值

clearstamps() 删除所有的stamp,默认无参数,删除所有

undo() 撤销上一步动作

speed() 乌龟爬行速度,我们这设置的是5,不设置为最快,直接生成

乌龟当前状态

position() | pos() 当前位置
towards() 返回与指定点之间的角度 参数:(X,Y)一个位置
xcor() 返回乌龟X坐标
ycor() 返回乌龟Y坐标
heading() 返回当前乌龟的方向值
distance() 返回乌龟与坐标点之间的距离。参数:(X,Y)一个位置

设置与测量

degrees() 设置整个圆的角度,最好不要动。参数:(integer or float)一个整数
radians() 将角度测量单位设置为弧度。360度就是2π

画笔控制

绘画状态

pendown() | pd() | down() 将笔落下放在图上,移动的时候将会绘图
penup() | pu() | up() 将笔提起来,移动的时候将不会绘图
pensize() | width() 设置线条的粗细。参数:(width)一个正数

pen() 使用键值对设置笔的属性

“shown”: True/False 显示
“pendown”: True/False 笔落下
“pencolor”: color-string or color-tuple 笔的颜色
“fillcolor”: color-string or color-tuple 填充颜色
“pensize”: positive number      笔大小(正整数)
“speed”: number in range 0..10    绘画速度(范围0-10)
“resizemode”: “auto” or “user” or “noresize” 大小调整模式
“stretchfactor”: (positive number, positive number) 拉伸参数
“outline”: positive number 外部
“tilt”: number 倾斜

isdown() 如果笔停止返回True,反之返回False

颜色控制

color() 颜色,直接使用返回当前笔颜色与填充颜色
pencolor() 设置笔的颜色
fillcolor() 设置笔的填充颜色

填充

filling() 返回填充状态,
begin_fill() 在填充之前使用
end_fill() 结束填充

更多绘画控制

reset() 重置所有参数
clear() 删除绘画,与reset不同之处仅仅是删除图形,参数保留
write() 写文字

arg ? object to be written to the TurtleScreen 写到TurtleScreen的参数
move ? True/False  移动
align ? one of the strings “left”, “center” or right” 对齐参数3选1(left,right,center)
font ? a triple (fontname, fontsize, fonttype) 字体

乌龟状态

可视性

showturtle() | st() 显示乌龟的形状
hideturtle() | ht() 隐藏乌龟的形状
isvisible() 是否可见,返回True or False

外表

shape() 设置乌龟的图形形状,可选( “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”)

resizemode() 大小调整模式

“auto”: adapts the appearance of the turtle corresponding to the value of pensize.   由画笔大小决定(自动)
“user”: adapts the appearance of the turtle according to the values of stretchfactor and outlinewidth (outline), 由拉伸参数决定
“noresize”: no adaption of the turtle's appearance takes place.            不调整

shapesize() | turtlesize() 返回笔的属性。
shearfactor() 设置或者返回当前剪切因子
settiltangle() 与tilt() 一样,只是可以为空,则返回当前旋转角度
tiltangle() 弃用
tilt() 设置当前乌龟角度,不调整乌龟前进方向(仅仅改变乌龟样子)
shapetransform() 设置或返回乌龟的形状的当前转换矩阵
get_shapepoly() 返回当前形状的坐标

监听动作

onclick() 鼠标点击事件

fun ? a function with two arguments which will be called with the coordinates of the clicked point on the canvas函数需要有两个参数
num ? number of the mouse-button, defaults to 1 (left mouse button) 单击次数,默认1
add ? True or False ? if True, a new binding will be added, otherwise it will replace a former binding 添加新的绑定函数,否则替代之前函数
例子:def turn(x, y):
  。。。 left(180)
onclick(turn)

onrelease() 鼠标释放事件,同上

ondrag() 鼠标移动事件,同上

乌龟一些特殊方法

begin_poly() 开始记录多边形的顶点,当前点为起始点
end_poly() 结束记录多边形的顶点,当前点为起始点
get_poly() 返回最后记录的多边形
clone() 复制一个一模一样的乌龟
getturtle() | getpen() 获取trutle对象本身
getscreen() 获取画布对象
setundobuffer() 设置或禁用中断器
undobufferentries() 返回undobuffer中的条目数

画布的方法

窗口控制

bgcolor() 设置或返回当前画布的背景颜色
bgpic() 设置或返回当前画布的背景图片名称
clear() | clearscreen() 清除图形
reset() | resetscreen() 重置画布

screensize() 画布大小

canvwidth ? positive integer, new width of canvas in pixels 宽度
canvheight ? positive integer, new height of canvas in pixels 高度
bg ? colorstring or color-tuple, new background color     颜色

setworldcoordinates() 全局坐标

llx ? a number, x-coordinate of lower left corner of canvas   左下X坐标
lly ? a number, y-coordinate of lower left corner of canvas   左下X坐标
urx ? a number, x-coordinate of upper right corner of canvas  右下X坐标
ury ? a number, y-coordinate of upper right corner of canvas  右下X坐标

动画控制

delay() 动画延迟(毫秒)参数:(integer )一个数字
tracer() 开启动画,设置延迟

n ? nonnegative integer    n个动作执行一次
delay ? nonnegative integer  延迟,毫秒

update() 更新画布,当tracer关闭时使用

画布监听

listen() 开启监听,将鼠标定位到画布
onkey() | onkeyrelease() 键盘弹起(需要位于焦点上,使用上面listen后)

fun ? a function with no arguments or None 动作函数
key ? a string: key (e.g. “a”) or key-symbol (e.g. “space”) 按键
onkeypress() 键盘按下事件,同上

onclick() | onscreenclick() 鼠标点击事件

fun ? a function with two arguments which will be called with the coordinates of the clicked point on the canvas 函数需要两个参数
num ? number of the mouse-button, defaults to 1 (left mouse button) 点击次数
add ? True or False ? if True, a new binding will be added, otherwise it will replace a former binding 是否是添加,还是替换
ontimer() 计时器

fun ? a function with no arguments 无需函数
t ? a number >= 0 事件间隔
mainloop() | done() 开始事件循环,必须是乌龟绘画中的最后一个函数

设置与特殊方法

mode() 绘图模式,3选1 “standard”, “logo” or “world”
colormode() 颜色模式, 1.0 或者 255
getcanvas() 返回当前TurtleScreen.的Canvas
getshapes() 返回当前可用形状
register_shape() | addshape() 3种调用方式。

1.直接调用图片。screen.register_shape(“turtle.gif”)

2.调用形状,制定点位置。

screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))

3,调用形状,名字随便取

turtles() 返回乌龟list数组
window_height() 返回窗口高度
window_width() 返回窗口宽度

输入方法

textinput() 文字输入

title ? string 输入名字
prompt ? string 输入的文本
numinput() 数字输入

title ? string 输入名字
prompt ? string 输入文本
default ? number (optional) 默认
minval ? number (optional) 最小
maxval ? number (optional) 最大

屏幕特有方法

bye() 关闭turtle窗口
exitonclick() 鼠标点击关闭窗口
setup() 设置主窗口参数

width ? if an integer, a size in pixels, if a float, a fraction of the screen; default is 50% of screen 宽 度
height ? if an integer, the height in pixels, if a float, a fraction of the screen; default is 75% of screen 高度
startx ? if positive, starting position in pixels from the left edge of the screen, if negative from the right edge, if None, center window horizontally 左边开始位置
startx ? if positive, starting position in pixels from the top edge of the screen, if negative from the bottom edge, if None, center window vertically 右边开始位置
title() 设置绘画窗口标题

以上这篇Python 用turtle实现用正方形画圆的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python判断、获取一张图片主色调的2个实例
Apr 10 Python
Python中Random和Math模块学习笔记
May 18 Python
使用Python编写基于DHT协议的BT资源爬虫
Mar 19 Python
python实现生成字符串大小写字母和数字的各种组合
Jan 01 Python
Django框架 querySet功能解析
Sep 04 Python
Python3使用PySynth制作音乐的方法
Sep 09 Python
Python tkinter布局与按钮间距设置方式
Mar 04 Python
如何对python的字典进行排序
Jun 19 Python
Python实现图片查找轮廓、多边形拟合、最小外接矩形代码
Jul 14 Python
Pycharm常用快捷键总结及配置方法
Nov 14 Python
Python连续赋值需要注意的一些问题
Jun 03 Python
python缺失值填充方法示例代码
Dec 24 Python
python 实现兔子生兔子示例
Nov 21 #Python
python3实现用turtle模块画一棵随机樱花树
Nov 21 #Python
用python中的matplotlib绘制方程图像代码
Nov 21 #Python
python hash每次调用结果不同的原因
Nov 21 #Python
python/Matplotlib绘制复变函数图像教程
Nov 21 #Python
Python 复平面绘图实例
Nov 21 #Python
Python3并发写文件与Python对比
Nov 20 #Python
You might like
默默简单的写了一个模板引擎
2007/01/02 PHP
PHP Pear 安装及使用
2009/03/19 PHP
PHP项目开发中最常用的自定义函数整理
2010/12/02 PHP
PHP中如何定义和使用常量
2013/02/28 PHP
php弹出对话框实现重定向代码
2014/01/23 PHP
基于thinkPHP框架实现留言板的方法
2016/10/17 PHP
Yii框架实现对数据库的CURD操作示例
2019/09/03 PHP
javascript模拟的Ping效果代码 (Web Ping)
2011/03/13 Javascript
jquery 添加节点的几种方法介绍
2013/09/04 Javascript
Jquery中国地图热点效果-鼠标经过弹出提示层信息的简单实例
2014/02/12 Javascript
jquery中的常用事件bind、hover、toggle等示例介绍
2014/07/21 Javascript
JS和jQuery使用submit方法无法提交表单的原因分析及解决办法
2016/05/17 Javascript
JQuery组件基于Bootstrap的DropDownList(完整版)
2016/07/05 Javascript
浅谈JavaScript中的this指针和引用知识
2016/08/05 Javascript
Bootstrap响应式表格详解
2017/05/23 Javascript
react 国际化的实现代码示例
2018/09/14 Javascript
详细解读Python中的__init__()方法
2015/05/02 Python
python使用Matplotlib绘制分段函数
2018/09/25 Python
详解Python 调用C# dll库最简方法
2019/06/20 Python
python实现ip地址的包含关系判断
2020/02/07 Python
Python监听键盘和鼠标事件的示例代码
2020/11/18 Python
Manduka官网:瑜伽垫、瑜伽毛巾和服装
2018/07/02 全球购物
红色连衣裙精品店:Red Dress Boutique
2018/08/11 全球购物
Right-on官方网站:日本知名的休闲服装品牌
2019/07/12 全球购物
英国医生在线预约:Top Doctors
2019/10/30 全球购物
应届生.NET方向面试题
2015/05/23 面试题
运动会开幕式邀请函
2014/01/22 职场文书
全国道德模范事迹
2014/02/01 职场文书
新品发布会主持词
2014/04/02 职场文书
中学生民族团结演讲稿
2014/08/27 职场文书
2014副镇长民主生活会个人对照检查材料思想汇报
2014/09/30 职场文书
学生穿着不得体检讨书
2014/10/12 职场文书
车间主任岗位职责范本
2015/04/08 职场文书
2015年乡镇纪委工作总结
2015/05/26 职场文书
2015年污水处理厂工作总结
2015/05/26 职场文书
Redis实现分布式锁的五种方法详解
2022/06/14 Redis