Python畫(huà)圖練習(xí)案例分享
1.多邊形的繪制案例
# 多邊形的繪制案例 import turtle def main(): turtle.color("green") # steps代表多邊形的繪制 turtle.circle(50,steps=6) turtle.exitonclick() if __name__ == "__main__": main()
2.太陽(yáng)花案例
# 太陽(yáng)花案例******************************************************************* 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.藝術(shù)圖片
# 藝術(shù)圖片************************************************************************* 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.繪制時(shí)鐘
#繪制時(shí)鐘************************************************************************************************ import turtle as tt from datetime import * # 當(dāng)前日期屬于一周的第幾天 def Week(t): week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] return week[t.weekday()] # 獲取當(dāng)前時(shí)間 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) # 移動(dòng)畫(huà)筆,距離為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) # 開(kāi)始記錄多邊形的頂點(diǎn) tt.begin_poly() tt.fd(length * 1.1) # 停止記錄多邊形的頂點(diǎn) tt.end_poly() # 返回記錄的多邊形 handForm = tt.get_poly() tt.register_shape(name, handForm) # 初始化 def initial(): global secHand, minHand, hurHand, printer # 重置方向向北(上),正角度為順時(shí)針 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() # 隱藏畫(huà)筆 printer.hideturtle() printer.penup() # 繪制表盤(pán)外框 def drawClock(R): # 清空窗口,重置turtule狀態(tài)為初始狀態(tài) tt.reset() # 畫(huà)筆尺寸 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: # 寫(xiě)文本 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: # 繪制指定半徑和顏色的點(diǎn) tt.dot(5, "red") movePen(-R) tt.right(6) # 表針的動(dòng)態(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)) # 設(shè)置當(dāng)前畫(huà)筆位置為原點(diǎn),方向朝東 printer.home() tt.tracer(True) # 經(jīng)過(guò)100ms后繼續(xù)調(diào)用handsMove函數(shù) tt.ontimer(handsMove, 100) # 調(diào)用定義的函數(shù),打開(kāi)和關(guān)閉動(dòng)畫(huà),為更新圖紙?jiān)O(shè)置延遲; tt.tracer(False) initial() drawClock(200) tt.tracer(True) handsMove() tt.mainloop()
7.繪制分形樹(shù)
# 繪制分形樹(shù)****************************************************************************** import turtle def draw_branch(branch_length): ''' 繪制分形樹(shù) ''' if branch_length > 5: # 繪制右側(cè)樹(shù)枝 turtle.forward(branch_length) print("向前:", branch_length) turtle.right(20) print("右轉(zhuǎn):20度") draw_branch(branch_length - 15) # 繪制左側(cè)樹(shù)枝 turtle.left(40) print("左轉(zhuǎn):40度") draw_branch(branch_length - 15) # 返回之前的樹(shù)枝 turtle.right(20) print("右轉(zhuǎn):20度") turtle.backward(branch_length) print("向后:", branch_length) def main(): ''' 主函數(shù) ''' 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()
到此這篇關(guān)于Python畫(huà)圖練習(xí)案例分享的文章就介紹到這了,更多相關(guān)Python畫(huà)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)python判斷是否回文數(shù)的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python判斷是否回文數(shù)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02一文教會(huì)你利用Python程序讀取Excel創(chuàng)建折線圖
不同類型的圖表有不同的功能,柱形圖主要用于對(duì)比數(shù)據(jù),折線圖主要用于展示數(shù)據(jù)變化的趨勢(shì),散點(diǎn)圖主要用于判斷數(shù)據(jù)的相關(guān)性,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一文教你利用Python程序讀取Excel創(chuàng)建折線圖的相關(guān)資料,需要的朋友可以參考下2022-11-11總結(jié)網(wǎng)絡(luò)IO模型與select模型的Python實(shí)例講解
同步、異步、阻塞、非阻塞,當(dāng)這些網(wǎng)絡(luò)IO名詞堆到一起時(shí)難免使編程初學(xué)者感到困惑,這里我們就來(lái)為大家總結(jié)網(wǎng)絡(luò)IO模型與select模型的Python實(shí)例講解:2016-06-06Numpy數(shù)組的組合與分割實(shí)現(xiàn)的方法
本文主要介紹了Numpy數(shù)組的組合與分割實(shí)現(xiàn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08matplotlib實(shí)戰(zhàn)之餅圖繪制詳解
餅圖,或稱餅狀圖,是一個(gè)劃分為幾個(gè)扇形的圓形統(tǒng)計(jì)圖表,這篇文章主要為大家詳細(xì)介紹了如何使用Matplotlib繪制餅圖,需要的小伙伴可以參考下2023-08-08python根據(jù)開(kāi)頭和結(jié)尾字符串獲取中間字符串的方法
這篇文章主要介紹了python根據(jù)開(kāi)頭和結(jié)尾字符串獲取中間字符串的方法,涉及Python操作字符串截取的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Python中的Descriptor描述符學(xué)習(xí)教程
簡(jiǎn)單來(lái)說(shuō),數(shù)據(jù)描述符是指實(shí)現(xiàn)了__get__、__set__、__del__方法的類屬性,等效于定義了三個(gè)方法的接口,下面就來(lái)詳細(xì)看一下Python中的Descriptor修飾符學(xué)習(xí)教程2016-06-06