Python画图练习案例分享

目录

1.多边形的绘制案例

2.太阳花案例

3.颜色五角星案例

4.艺术图片

5.黑六边形

6.绘制时钟

7.绘制分形树

8.彩虹线绘制案例

1.多边形的绘制案例 # 多边形的绘制案例 import turtle def main(): turtle.color("green") # steps代表多边形的绘制 turtle.circle(50,steps=6) turtle.exitonclick() if __name__ == "__main__": main()

2.太阳花案例 # 太阳花案例******************************************************************* import turtle import time turtle.color("red","yellow") turtle.begin_fill() for _ in range(50): turtle.speed(0) turtle.forward(200) turtle.left(170) turtle.end_fill() turtle.mainloop()

3.颜色五角星案例 # 颜色五角星案例****************************************************************** import turtle import time turtle.pensize(5) turtle.pencolor("yellow") turtle.fillcolor("red") turtle.begin_fill() for _ in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill() time.sleep(2) turtle.penup() turtle.goto(-150,-120) turtle.color("violet") turtle.write("Done",font=("Arial")) turtle.mainloop()

4.艺术图片 # 艺术图片************************************************************************* import turtle turtle.speed(0) turtle.delay(0) turtle.pensize(2) turtle.bgcolor("black") colors=["red","blue","yellow","purple"] for x in range(300): turtle.color(colors[x%4]) turtle.forward(2*x) turtle.left(91) turtle.done()

5.黑六边形 # #黑六边形***************************************************************************** import turtle def bye(x,y): turtle.bye() s = turtle.Screen() s.bgcolor("black") s.screensize(800,800) s.title("Class Using") s.onscreenclick(bye) p=turtle.Turtle() p.speed(0) p.hideturtle() p.pencolor("red") p.pensize(3) p.circle(50,360,6) turtle.done()

前方高能

6.绘制时钟 #绘制时钟************************************************************************************************ 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()

7.绘制分形树 # 绘制分形树****************************************************************************** import turtle def draw_branch(branch_length): ''' 绘制分形树 ''' if branch_length > 5: # 绘制右侧树枝 turtle.forward(branch_length) print("向前:", branch_length) turtle.right(20) print("右转:20度") draw_branch(branch_length - 15) # 绘制左侧树枝 turtle.left(40) print("左转:40度") draw_branch(branch_length - 15) # 返回之前的树枝 turtle.right(20) print("右转:20度") turtle.backward(branch_length) print("向后:", branch_length) def main(): ''' 主函数 ''' turtle.speed(0.5) turtle.pensize(3) turtle.left(90) turtle.color('green') turtle.penup() turtle.backward(150) turtle.pendown() turtle.bgcolor("black") draw_branch(100) turtle.exitonclick() if __name__ == "__main__": main()

8.彩虹线绘制案例 # 彩虹线绘制案例*************************************************************************** import turtle as t from random import randint as rint t.shape("turtle") t.pensize(5) t.colormode(255) t.bgcolor("black") t.tracer(False) for x in range(700): t.color(rint(0,255),rint(0,255),rint(0,255)) t.circle(2*(1+x/4),5) t.speed(0) t.tracer(True) t.exitonclick()

到此这篇关于Python画图练习案例分享的文章就介绍到这了,更多相关Python画图内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读