前几天学习了一下python的turtle库,它是python中一个绘制图像的函数库,用海龟可以画出各种图像,学习之后我画了可爱的小黄人,和太阳等图案,觉得很好玩很有趣,在这里想介绍一下turtle的使用详解,感兴趣或者需要的朋友可以参考一下。

首先说明一下turtle绘图的基础知识:

1. 画布(canvas)
画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置
1.1 设置画布大小
(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.1 画笔的状态
在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟. 这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态
2.2 画笔的属性
画笔(画笔的属性,颜色、画线的宽度)
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 绘图命令
操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令
(1)画笔运动命令:

(2)画笔控制命令:

(3) 全局控制命令

3. 命令详解
3.1 turtle.circle(radius, extent=None, steps=None)
描述: 以给定半径画圆
参数:
radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)

4. 绘图实例

4.1 太阳花

import turtle as t
import time
t.color("red","yellow")
t.speed(10)
t.begin_fill()
for x in range(50):t.forward(200)t.left(170)
end_fill()
time.sleep(2)

4.2 彩虹线

import turtle as t
from random import randint as rint
t.shape("turtle")
t.pensize(5)
t.colormode(255)
t.bgcolor("black")
t.tracer(False)
for x in range(700):t.color(rint(0,255),rint(0,255),rint(0,255))t.circle(2*(1+x/4),5)t.speed(30)t.tracer(True)

4.3 小黄人

  import turtle
t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255)
t.hideturtle()
t.speed(10)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()t.left(180)
t.goto(70,30)t.dot()#脚
t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.end_fill()

python treeview控件使用详解_python绘图工具turtle库的使用详解相关推荐

  1. 海龟编程 python绘图工具turtle库的用法 turtle库使用方法大全,画笔设置 画布设置 绘图设置,画笔粗细,画笔颜色, 画笔速度。Python二级必须会的命令(已获取证书)

    目录 海龟编程 python绘图工具turtle库的用法 画布: 画笔 画笔运动命令: 画笔的控制命令: 全局控制命令: 简单turtle绘图示例: 圆中方: 三色同心圆: 四个圆中方: 螺旋正方: ...

  2. python treeview控件使用详解,python Treeview使用笔记 1

    先贴代码,python2.7 #!/usr/bin/env python # -*- coding:utf-8 -*- import ttk from Tkinter import * class t ...

  3. python如何设置画布开始位置_Python绘图篇——Turtle库详解(一)

    交流学习python编程技术及行业动态,点击上方"蓝字"关注我们 Python绘图篇--Turtle库详解(一) 写在前面 ↓ 注:本教程基于64位windows系统(鼠标右键点击 ...

  4. python画圆弧组成的花瓣代码_Python编程的Turtle 库画出“精美碎花小清新风格树”,速取代码!...

    作者 | 1_bit 责编 | 王晓曼 出品 | CSDN博客 开始 使用Turtle画树,看了一下网上的代码,基本上核心的方法是使用递归:其次通过递归传参更笔的粗细从而改变绘制时的线段,更改树的躯干 ...

  5. Python Tkinter控件之 Label 详解

    (Label)标签控件被用于显示文本和图像.标签可包含多行文本,但只能用一种字体. Python Tkinter 标签控件(Label):指定的窗口中显示的文本和图像 注:你如果需要显示一行或多行文本 ...

  6. WPF 控件专题 TreeView控件详解

    1.TreeView介绍 命名空间:System.Windows.Controls TreeView 表示一个控件,该控件在树结构(其中的项可以展开和折叠)中显示分层数据. TreeView 是一个 ...

  7. treeview控件使用详解python_TreeView控件实践

    TreeView控件可以通过HierarchicalDataTemplate 和 DataTemplate来自定义. 1)HierarchicalDataTemplate用来支持HeaderedIte ...

  8. python tkinter控件treeview的数据列表显示的实现_code

    素材文件 result.txt result2.txt result.txt文件的数据来源是爬取猫眼电影前一百名的电影,而result2.txt文件只不过是内容上把result.txt的内容复制几十次 ...

  9. Python GUI编程入门(31)-Treeview控件

    Treeview是Tkinter8.5新引入的控件,可以用于实现较为复杂的GUI界面.本文使用一个FileBrowser实例来说明它的用法. 构建主窗口和退出菜单的代码和前一篇文章几乎相同: root ...

  10. java treeview控件_【TreeView下载】TreeView控件 v1.1.6 官方版-开心电玩

    软件介绍 TreeView控件是一款有Java语言编写的代码显示工具,这款软件将用户要观看的代码以树状结构显示,支持以节点的方式观看代码,让代码的现实更清晰.更有条理,让用户可以更好的了解代码结构,欢 ...

最新文章

  1. c++ 构造函数析构函数 数据安全_C++知识点 16:构造函数和析构函数的语法
  2. [转] GDBT详解
  3. 深入理解面向对象设计的七大原则
  4. 配置SQL Server的命名管道和TCP/IP设置
  5. 如何清除图片下方出现几像素的空白间隙?
  6. HDU2030 汉字统计【入门】
  7. Delphi Sockets.pas单元中TIpSocket的Bug
  8. Java通过JNI/JNA加载dll库文件调用C接口,出现“java.lang.UnsatisfiedLinkError: no XXX in java.library.path”问题
  9. GIS+区块链,地理空间数据新型应用场景未来可期
  10. Linux-文件打开数配置实践
  11. Boom 3D 1.2.2 特别版 Mac 3D环绕音效增强工具
  12. php过气了吗,她怎么就过气了?
  13. postgresql垃圾清理插件pg_repack
  14. jbox弹窗_Jquery多功能提示通知弹出对话框插件jBox中文文档
  15. ssm基于javaweb的医疗健康知识管理系统设计与实现 毕业设计-附源码131903
  16. 百思不得姐之广告界面(三)
  17. Python:计算及格率和优秀率,用百分数表示,百分号前的部分四舍五入保留整数
  18. xx-xx-xx-xx转换成x年x月x日星期x
  19. Go语言管道与高并发实战
  20. 实验室天平台制作要求

热门文章

  1. Verilog中的specify block和timing check
  2. [!] Unable to satisfy the following requirements:
  3. linux 命令 —— cp
  4. 域用户权限|连接DC终端服务
  5. 2010.11.13网规论文关注点
  6. Spring Cloud(Greenwich版)-03-编写高可用Eureka Server(集群)
  7. 异步和同步的机制问题
  8. nodejs 中的 commonjs 模块化使用
  9. 第一天:搭建环境和初始化路由
  10. 微信公众号发送消息接口(群发接口)