Python中turtle库的使用实例


Posted in Python onSeptember 09, 2019

Turtle库是Python内置的图形化模块,属于标准库之一,位于Python安装目录的lib文件夹下,常用函数有以下几种:

画笔控制函数

  • penup():抬起画笔;
  • pendown():落下画笔;
  • pensize(width):画笔宽度;
  • pencolor(color):画笔颜色;

运动控制函数

  • forward(d)/fd(d):直行d个像素;
  • circle(r, extent = None):绘制半径为r,角度为extent的弧形,圆心默认在海龟左侧距离r的位置;

方向控制函数

  • setheading(angle)/seth(angle):改变前进方向;
  • left(angle):海龟左转;
  • right(angle):海龟右转;

Turtle库的使用

#coding=utf-8
#绘制蟒蛇
import turtle
turtle.penup()
turtle.pencolor("red")
turtle.forward(-250)
turtle.pendown()
turtle.pensize(10)
turtle.right(45)
for i in range(4):
  turtle.circle(40, 80)
  turtle.circle(-40, 80)
turtle.circle(40, 80 / 2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2 / 3)
turtle.done()

结果

Python中turtle库的使用实例

#coding=utf-8
# 绘制五角星
import turtle
turtle.pensize(5)
turtle.pencolor("red")
turtle.forward(200)
for i in range(4):
  turtle.right(144)
  turtle.fd(200)
turtle.done()

结果

Python中turtle库的使用实例

#绘制时钟
# coding=utf-8
import turtle as tt
from datetime import *

# 当前日期属于一周的第几天
def Week(t):
  week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
  return week[t.weekday()]

# 获取当前时间
def Date(t):
  y = t.year
  m = t.month
  d = t.day
  cur_hour = t.hour;
  cur_min = t.minute;
  cur_sec = t.second;
  return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec)

# 移动画笔,距离为distance
def movePen(distance):
  tt.penup()
  tt.pensize(5)
  tt.pencolor("blue")
  tt.fd(distance)
  tt.pendown()

# 绘制表针
def makeHands(name, length):
  # 清空窗口,重置turtule状态为初始状态
  tt.reset()
  movePen(-length * 0.1)
  # 开始记录多边形的顶点
  tt.begin_poly()
  tt.fd(length * 1.1)
  # 停止记录多边形的顶点
  tt.end_poly()
  # 返回记录的多边形
  handForm = tt.get_poly()
  tt.register_shape(name, handForm)

# 初始化
def initial():
  global secHand, minHand, hurHand, printer
  # 重置方向向北(上),正角度为顺时针
  tt.mode("logo")
  # 建立并初始化表针
  makeHands("secHand", 180)
  makeHands("minHand", 150)
  makeHands("hurHand", 110)
  secHand = tt.Turtle()
  secHand.shape("secHand")
  minHand = tt.Turtle()
  minHand.shape("minHand")
  hurHand = tt.Turtle()
  hurHand.shape("hurHand")

  for hand in secHand, minHand, hurHand:
    hand.shapesize(1, 1, 4)
    hand.speed(0)

  # 输出文字
  printer = tt.Turtle()
  # 隐藏画笔
  printer.hideturtle()
  printer.penup()

# 绘制表盘外框
def drawClock(R):
  # 清空窗口,重置turtule状态为初始状态
  tt.reset()
  # 画笔尺寸
  tt.pensize(5)
  for i in range(60):
    movePen(R)
    if i % 5 == 0:
      tt.fd(20)
      movePen(-R - 20)

      movePen(R + 20)
      if i == 0:
        # 写文本
        tt.write(int(12), align="center", font=("Consolas", 14, "bold"))
      elif i == 30:
        movePen(25)
        tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
        movePen(-25)
      elif (i == 25 or i == 35):
        movePen(20)
        tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
        movePen(-20)
      else:
        tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
      movePen(-R - 20)
    else:
      # 绘制指定半径和颜色的点
      tt.dot(5, "red")
      movePen(-R)
    tt.right(6)

# 表针的动态显示
def handsMove():
  t = datetime.today()
  second = t.second + t.microsecond * 0.000001
  minute = t.minute + second / 60.0
  hour = t.hour + minute / 60.0
  secHand.seth(6 * second)
  minHand.seth(6 * minute)
  hurHand.seth(30 * hour)

  tt.tracer(False)
  printer.fd(65)
  tt.pencolor("green")
  printer.write(Week(t), align="center", font = ("黑体", 14))
  printer.back(130)
  printer.write(Date(t), align="center", font = ("Consolas", 14))
  # 设置当前画笔位置为原点,方向朝东
  printer.home()
  tt.tracer(True)

  # 经过100ms后继续调用handsMove函数
  tt.ontimer(handsMove, 100)

# 调用定义的函数,打开和关闭动画,为更新图纸设置延迟;
tt.tracer(False)
initial()
drawClock(200)
tt.tracer(True)
handsMove()
tt.mainloop()

结果

Python中turtle库的使用实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python通过matplotlib绘制动画简单实例
Dec 13 Python
Python hashlib模块用法实例分析
Jun 12 Python
python爬虫之线程池和进程池功能与用法详解
Aug 02 Python
在Pycharm中对代码进行注释和缩进的方法详解
Jan 20 Python
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
May 23 Python
Python之修改图片像素值的方法
Jul 03 Python
python SQLAlchemy 中的Engine详解
Jul 04 Python
详解Python用三种方式统计词频的方法
Jul 29 Python
Pytorch 之修改Tensor部分值方式
Dec 27 Python
基于tensorflow指定GPU运行及GPU资源分配的几种方式小结
Feb 03 Python
jupyter notebook oepncv 显示一张图像的实现
Apr 24 Python
python如何利用cv2模块读取显示保存图片
Jun 04 Python
Django之路由层的实现
Sep 09 #Python
python中web框架的自定义创建
Sep 08 #Python
python web框架中实现原生分页
Sep 08 #Python
python中open函数的基本用法示例
Sep 07 #Python
Python3显示当前时间、计算时间差及时间加减法示例代码
Sep 07 #Python
利用python计算时间差(返回天数)
Sep 07 #Python
Django配置MySQL数据库的完整步骤
Sep 07 #Python
You might like
哪吒敖丙传:新人物二哥敖乙出场 小敖丙奶气十足
2020/03/08 国漫
php日历[测试通过]
2008/03/27 PHP
PHP 最大运行时间 max_execution_time修改方法
2010/03/08 PHP
php json与xml序列化/反序列化
2013/10/28 PHP
PHP echo,print,printf,sprintf函数之间的区别与用法详解
2013/11/27 PHP
php实现使用正则将文本中的网址转换成链接标签
2014/12/03 PHP
js如何取消事件冒泡
2013/09/23 Javascript
JQuery CheckBox(复选框)操作方法汇总
2015/04/15 Javascript
json传值以及ajax接收详解
2016/05/24 Javascript
javascript如何创建对象
2016/08/29 Javascript
knockoutjs动态加载外部的file作为component中的template数据源的实现方法
2016/09/01 Javascript
html5+CSS 实现禁止IOS长按复制粘贴功能
2016/12/28 Javascript
详解Vue用自定义指令完成一个下拉菜单(select组件)
2017/10/31 Javascript
vue引用js文件的多种方式(推荐)
2018/05/17 Javascript
JavaScript 扩展运算符用法实例小结【基于ES6】
2019/06/17 Javascript
JS+CSS实现3D切割轮播图
2020/03/21 Javascript
如何在Express4.x中愉快地使用async的方法
2020/11/18 Javascript
详解vite2.0配置学习(typescript版本)
2021/02/25 Javascript
python pdb调试方法分享
2014/01/21 Python
浅谈Python实现Apriori算法介绍
2017/12/20 Python
Python zip()函数用法实例分析
2018/03/17 Python
基于python进行抽样分布描述及实践详解
2019/09/02 Python
python处理RSTP视频流过程解析
2020/01/11 Python
python多线程实现代码(模拟银行服务操作流程)
2020/01/13 Python
python sorted函数原理解析及练习
2020/02/10 Python
Django choices下拉列表绑定实例
2020/03/13 Python
css3实现wifi信号逐渐增强效果实例
2017/08/09 HTML / CSS
html5 canvas-1.canvas介绍(hello canvas)
2013/01/07 HTML / CSS
HTML5适合的情人节礼物有纪念日期功能
2021/01/25 HTML / CSS
英国领先的电子、技术和办公用品购物网站:Ebuyer
2018/04/04 全球购物
汉森冲浪板:Hansen Surfboards
2018/05/19 全球购物
动物学专业毕业生求职信
2013/10/11 职场文书
企业统计员岗位职责
2013/12/13 职场文书
干部下基层实施方案
2014/03/14 职场文书
离婚民事起诉状
2015/08/03 职场文书
windows11怎么查看wifi密码? win11查看wifi密码的技巧
2021/11/21 数码科技