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开发的HTTP库requests详解

Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网...

PyQt5通信机制 信号与槽详解

PyQt5通信机制 信号与槽详解

 前言 信号和槽是PyQt编程对象之间进行通信的机制。每个继承自QWideget的控件都支持信号与槽机制。信号发射时(发送请求),连接的槽函数就会自动执行(针对请求进行处理)。...

python requests.post带head和body的实例

如下所示: # coding = utf-8 import requests import json host = "http://47.XX.XX.XX:30000" endpo...

python2和python3应该学哪个(python3.6与python3.7的选择)

首先先说一下python2与python3的选择 许多刚入门 Python 的朋友都在纠结的的问题是:我应该选择学习 python2 还是 python3? 对此,回答是:果断 Pyth...

python中的常量和变量代码详解

局部和全局变量: # name='lhf' # def change_name(): # # global name # name='帅了一比' # print('cha...