Python畫圖練習案例分享
更新時間:2022年07月06日 11:29:48 作者:王小王_123???????
這篇文章主要介紹了Python畫圖練習案例分享,文章基于Python實現各種畫圖,具有一定的參考價值,感興趣的小伙伴可以參考一下
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狀態(tài)為初始狀態(tài) 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狀態(tài)為初始狀態(tài) 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) # 表針的動態(tài)顯示 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后繼續(xù)調用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畫圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一文教會你利用Python程序讀取Excel創(chuàng)建折線圖
不同類型的圖表有不同的功能,柱形圖主要用于對比數據,折線圖主要用于展示數據變化的趨勢,散點圖主要用于判斷數據的相關性,下面這篇文章主要給大家介紹了關于如何通過一文教你利用Python程序讀取Excel創(chuàng)建折線圖的相關資料,需要的朋友可以參考下2022-11-11