Python+Turtle动态绘制一棵树实例分享

 更新时间:2018年01月16日 14:28:58   作者:wangsiting123  
这篇文章主要介绍了Python+Turtle动态绘制一棵树实例分享,具有一定借鉴价值,需要的朋友可以参考下

本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码:

# drawtree.py
 
from turtle import Turtle, mainloop
 
def tree(plist, l, a, f):
  """ plist is list of pens
  l is length of branch
  a is half of the angle between 2 branches
  f is factor by which branch is shortened
  from level to level."""
  if l > 5: #
    lst = []
    for p in plist:
      p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
      q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
      p.left(a) #Turn turtle left by angle units
      q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
      lst.append(p)#将元素增加到列表的最后
      lst.append(q)
    tree(lst, l*f, a, f)
  
      
 
def main():
  p = Turtle()
  p.color("green")
  p.pensize(5)
  #p.setundobuffer(None)
  p.hideturtle() #Make the turtle invisible. It's a good idea to do this while you're in the middle of doing some complex drawing,
  #because hiding the turtle speeds up the drawing observably.
  #p.speed(10)
  # p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
  p.speed(10)
  #TurtleScreen methods can then be called for that object.
  p.left(90)# Turn turtle left by angle units. direction 调整画笔
 
  p.penup() #Pull the pen up – no drawing when moving.
  p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle's orientation.
  p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
  #否则turtle一移动就会自动的把线画出来
 
  #t = tree([p], 200, 65, 0.6375)
  t = tree([p], 200, 65, 0.6375)
   
main()

实现效果:

总结

以上就是本文关于Python+Turtle动态绘制一棵树实例分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • 详解分布式系统中如何用python实现Paxos

    详解分布式系统中如何用python实现Paxos

    提到分布式算法,就不得不提 Paxos 算法,在过去几十年里,它基本上是分布式共识的代 名词,因为当前最常用的一批共识算法都是基于它改进的。比如,Fast Paxos 算法、 Cheap Paxos 算法、Raft 算法、ZAB 协议等等。
    2021-05-05
  • 对pyqt5中QTabWidget的相关操作详解

    对pyqt5中QTabWidget的相关操作详解

    今天小编就为大家分享一篇对pyqt5中QTabWidget的相关操作详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Python使用re模块实现正则表达式操作指南

    Python使用re模块实现正则表达式操作指南

    在Python中需要通过正则表达式对字符串进⾏匹配的时候,可以使⽤⼀个python自带的模块,名字为re,下面这篇文章主要给大家介绍了关于Python使用re模块实现正则表达式操作的相关资料,需要的朋友可以参考下
    2022-07-07
  • 使用selenium和pyquery爬取京东商品列表过程解析

    使用selenium和pyquery爬取京东商品列表过程解析

    这篇文章主要介绍了使用selenium和pyquery爬取京东商品列表过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python pytest.main()运行测试用例

    Python pytest.main()运行测试用例

    这篇文章主要介绍了Python pytest.main()运行测试用例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12
  • python实现中文输出的两种方法

    python实现中文输出的两种方法

    这篇文章主要介绍了python实现中文输出的两种方法,实例分析了Python操作中文输出的技巧,需要的朋友可以参考下
    2015-05-05
  • pycharm运行程序时看不到任何结果显示的解决

    pycharm运行程序时看不到任何结果显示的解决

    今天小编就为大家分享一篇pycharm运行程序时看不到任何结果显示的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 用代码帮你了解Python基础(3)

    用代码帮你了解Python基础(3)

    这篇文章主要用代码帮你了解Python基础,使用循环,字典和集合的示例代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Python算法应用实战之队列详解

    Python算法应用实战之队列详解

    队列是一种先进先出(First-In-First-Out,FIFO)的数据结构。队列被用在很多地方,比如提交操作系统执行的一系列进程、打印任务池等,一些仿真系统用队列来模拟银行或杂货店里排队的顾客。下面就介绍了Python中队列的应用实战,需要的可以参考。
    2017-02-02
  • Python装饰器的函数式编程详解

    Python装饰器的函数式编程详解

    本文向大家详细介绍了Python装饰器的函数式编程的相关资料,需要的朋友可以参考下
    2015-02-02

最新评论