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 第一步 hello world
Sep 25 Python
Python3基础之函数用法
Aug 13 Python
Python中使用logging模块打印log日志详解
Apr 05 Python
将Python的Django框架与认证系统整合的方法
Jul 24 Python
Python语言生成水仙花数代码示例
Dec 18 Python
解决python selenium3启动不了firefox的问题
Oct 13 Python
基于django ManyToMany 使用的注意事项详解
Aug 09 Python
pytorch 预训练层的使用方法
Aug 20 Python
Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】
Oct 12 Python
python输出结果刷新及进度条的实现操作
Jul 13 Python
python判断字符串以什么结尾的实例方法
Sep 18 Python
总结Python连接CS2000的详细步骤
Jun 23 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
PHP 5.0对象模型深度探索之对象复制
2008/03/27 PHP
PHP程序61条面向对象分析设计的经验小结
2008/11/12 PHP
PHP中调用SVN命令更新网站方法
2015/01/07 PHP
php根据用户语言跳转相应网页
2015/11/04 PHP
PHP获取真实IP及IP模拟方法解析
2020/11/24 PHP
JavaScript 事件参考手册
2008/12/24 Javascript
JavaScript中的style.cssText使用教程
2014/11/06 Javascript
js实现图片从左往右渐变切换效果的方法
2015/02/06 Javascript
AngularJS中如何使用$http对MongoLab数据表进行增删改查
2016/01/23 Javascript
JavaScript中的编码和解码函数
2017/02/15 Javascript
vue iview组件表格 render函数的使用方法详解
2018/03/15 Javascript
javaScript产生随机数的用法小结
2018/04/21 Javascript
vue-router源码之history类的浅析
2019/05/21 Javascript
怎么使用javascript深度拷贝一个数组
2019/06/06 Javascript
微信小程序picker组件两列关联使用方式
2020/10/27 Javascript
[03:40]DOTA2英雄梦之声_第01期_炼金术士
2014/06/23 DOTA
[32:30]夜魇凡尔赛茶话会 第一期01:谁是卧底
2021/03/11 DOTA
Python version 2.7 required, which was not found in the registry
2014/08/26 Python
用Python实现筛选文件脚本的方法
2018/10/27 Python
python获取依赖包和安装依赖包教程
2020/02/13 Python
基于python-pptx库中文文档及使用详解
2020/02/14 Python
Keras 在fit_generator训练方式中加入图像random_crop操作
2020/07/03 Python
Python实现一个简单的递归下降分析器
2020/08/01 Python
浅谈Python 钉钉报警必备知识系统讲解
2020/08/17 Python
解决html5中video标签无法播放mp4问题的办法
2017/05/07 HTML / CSS
SneakerStudio英国:最佳运动鞋商店
2019/05/22 全球购物
学生党员思想汇报
2013/12/28 职场文书
最新个人职业生涯规划书
2014/01/22 职场文书
平安家庭示范户事迹
2014/06/02 职场文书
奥巴马上海演讲稿
2014/09/10 职场文书
大学军训通讯稿
2015/07/18 职场文书
公共场所卫生管理制度
2015/08/05 职场文书
保护环境建议书作文300字
2015/09/14 职场文书
2016春季运动会前导词
2015/11/25 职场文书
SQL Server内存机制浅探
2022/04/06 SQL Server
Redis全局ID生成器的实现
2022/06/05 Redis