python使用turtle库绘制树

yipeiwu_com5年前Python基础

本文实例为大家分享了python使用turtle库绘制树的具体代码,供大家参考,具体内容如下

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import turtle, datetime 
def drawGap(): #绘制数码管间隔 
  turtle.penup() 
  turtle.fd(5) 
def drawLine(draw):  #绘制单段数码管 
  drawGap() 
  turtle.pendown() if draw else turtle.penup() 
  turtle.fd(40) 
  drawGap() 
  turtle.right(90) 
def drawDigit(d): #根据数字绘制七段数码管 
  drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False) 
  drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False) 
  drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False) 
  drawLine(True) if d in [0,2,6,8] else drawLine(False) 
  turtle.left(90) 
  drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False) 
  drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False) 
  drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False) 
  turtle.left(180) 
  turtle.penup() 
  turtle.fd(20) 
def drawDate(date): 
  turtle.pencolor("red") 
  for i in date: 
    if i == '-': 
      turtle.write('年',font=("Arial", 18, "normal")) 
      turtle.pencolor("green") 
      turtle.fd(40) 
    elif i == '=': 
      turtle.write('月',font=("Arial", 18, "normal")) 
      turtle.pencolor("blue") 
      turtle.fd(40) 
    elif i == '+': 
      turtle.write('日',font=("Arial", 18, "normal")) 
    else: 
      drawDigit(eval(i)) 
def main(): 
  turtle.setup(800, 350, 200, 200) 
  turtle.penup() 
  turtle.fd(-350) 
  turtle.pensize(5) 
  drawDate(datetime.datetime.now().strftime('%Y-%m=%d+')) 
  turtle.hideturtle() 
main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python脚本设置超时机制系统时间的方法

python脚本设置超时机制系统时间的方法

本文为大家介绍了python脚本设置系统时间的方法,一共有两种,其一是调用socket直接发送udp包到国家授时中心,其二是调用ntplib包。我在本地电脑ping 国家授时中心地址cn...

python程序 创建多线程过程详解

一、python线程的模块 1.1 thread和threading模块 thread模块提供了基本的线程和锁的支持 threading提供了更高级别、功能更强的线程管理的功能。...

python内存监控工具memory_profiler和guppy的用法详解

python2.7在内存管理上相比python3还是有些坑的,其释放后的内存仍然保留在python的内存池中,不被系统所用。python循环引用的变量不会被回收,这会导致程序越运行,占用...

Python实现的基数排序算法原理与用法实例分析

Python实现的基数排序算法原理与用法实例分析

本文实例讲述了Python实现的基数排序算法。分享给大家供大家参考,具体如下: 基数排序(radix sort)属于“分配式排序”(distribution sort),又称“桶子法”(...

python监测当前联网状态并连接的实例

如下所示: def test1(): import os return1=os.system('ping 8.8.8.8') if return1: print 'ping...