Python內(nèi)置模塊turtle繪圖詳解
urtle庫是Python語言中一個(gè)很流行的繪制圖像的函數(shù)庫,想象一個(gè)小烏龜,在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)位置開始,它根據(jù)一組函數(shù)指令的控制,在這個(gè)平面坐標(biāo)系中移動(dòng),從而在它爬行的路徑上繪制了圖形。
turtle繪圖的基礎(chǔ)知識(shí):
1.畫布(canvas)
畫布就是turtle為我們展開用于繪圖區(qū)域,我們可以設(shè)置它的大小和初始位置。
設(shè)置畫布大小
turtle.screensize(canvwidth=None,canvheight=None,bg=None),參數(shù)分別為畫布的寬(單位像素),高,背景顏色。
如:turtle.screensize(800,600,"green")
turtle.screensize()#返回默認(rèn)大小(400,300)
turtle.setup(width=0.5,height=0.75,startx=None,starty=None),參數(shù):width,height:輸入寬和高為整數(shù)時(shí),表示像素;為小數(shù)時(shí),表示占據(jù)電腦屏幕的比例,(startx,starty):這一坐標(biāo)表示矩形窗口左上角頂點(diǎn)的位置,如果為空,則窗口位于屏幕中心。
如:turtle.setup(width=0.6,height=0.6)
turtle.setup(width=800,height=800,startx=100,starty=100)
2.畫筆
2.1畫筆的狀態(tài)
在畫布上,默認(rèn)有一個(gè)坐標(biāo)原點(diǎn)為畫布中心的坐標(biāo)軸,坐標(biāo)原點(diǎn)上有一只面朝x軸正方向小烏龜。這里我們描述小烏龜時(shí)使用了兩個(gè)詞語:坐標(biāo)原點(diǎn)(位置),面朝x軸正方向(方向),turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態(tài)。
2.2畫筆的屬性
畫筆(畫筆的屬性,顏色、畫線的寬度等)
1)turtle.pensize():設(shè)置畫筆的寬度;
2)turtle.pencolor():沒有參數(shù)傳入,返回當(dāng)前畫筆顏色,傳入?yún)?shù)設(shè)置畫筆顏色,可以是字符串如"green","red",也可以是RGB3元組。
3)turtle.speed(speed):設(shè)置畫筆移動(dòng)速度,畫筆繪制的速度范圍[0,10]整數(shù),數(shù)字越大越快。
2.3繪圖命令
操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:一種為運(yùn)動(dòng)命令,一種為畫筆控制命令,還有一種是全局控制命令。
(1)畫筆運(yùn)動(dòng)命令
命令 |
說明 |
turtle.forward(distance) |
向當(dāng)前畫筆方向移動(dòng)distance像素長度 |
turtle.backward(distance) |
向當(dāng)前畫筆相反方向移動(dòng)distance像素長度 |
turtle.right(degree) |
順時(shí)針移動(dòng)degree° |
turtle.left(degree) |
逆時(shí)針移動(dòng)degree° |
turtle.pendown() |
移動(dòng)時(shí)繪制圖形,缺省時(shí)也為繪制 |
turtle.goto(x,y) |
將畫筆移動(dòng)到坐標(biāo)為x,y的位置 |
turtle.penup() |
提起筆移動(dòng),不繪制圖形,用于另起一個(gè)地方繪制 |
turtle.circle() |
畫圓,半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓 |
setx( ) |
將當(dāng)前x軸移動(dòng)到指定位置 |
sety( ) |
將當(dāng)前y軸移動(dòng)到指定位置 |
setheading(angle) |
設(shè)置當(dāng)前朝向?yàn)閍ngle角度 |
home() |
設(shè)置當(dāng)前畫筆位置為原點(diǎn),朝向東。 |
dot(r) |
繪制一個(gè)指定直徑和顏色的圓點(diǎn) |
(2) 畫筆控制命令
命令 |
說明 |
turtle.fillcolor(colorstring) |
繪制圖形的填充顏色 |
turtle.color(color1, color2) |
同時(shí)設(shè)置pencolor=color1, fillcolor=color2 |
turtle.filling() |
返回當(dāng)前是否在填充狀態(tài) |
turtle.begin_fill() |
準(zhǔn)備開始填充圖形 |
turtle.end_fill() |
填充完成 |
turtle.hideturtle() |
隱藏畫筆的turtle形狀 |
turtle.showturtle() |
顯示畫筆的turtle形狀 |
(3) 全局控制命令
命令 |
說明 |
turtle.clear() |
清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變 |
turtle.reset() |
清空窗口,重置turtle狀態(tài)為起始狀態(tài) |
turtle.undo() |
撤銷上一個(gè)turtle動(dòng)作 |
turtle.isvisible() |
返回當(dāng)前turtle是否可見 |
stamp() |
復(fù)制當(dāng)前圖形 |
turtle.write(s [,font=("font-name",font_size,"font_type")]) |
寫文本,s為文本內(nèi)容,font是字體的參數(shù),分別為字體名稱,大小和類型;font為可選項(xiàng),font參數(shù)也是可選項(xiàng) |
(4) 其他命令
命令 |
說明 |
|||||||||
turtle.mainloop()或turtle.done() |
啟動(dòng)事件循環(huán) -調(diào)用Tkinter的mainloop函數(shù)。 必須是烏龜圖形程序中的最后一個(gè)語句。 |
|||||||||
turtle.mode(mode=None) |
設(shè)置烏龜模式(“standard”,“l(fā)ogo”或“world”)并執(zhí)行重置。如果沒有給出模式,則返回當(dāng)前模式。
|
|||||||||
turtle.delay(delay=None) |
設(shè)置或返回以毫秒為單位的繪圖延遲。 |
|||||||||
turtle.begin_poly() |
開始記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的第一個(gè)頂點(diǎn)。 |
|||||||||
turtle.end_poly() |
停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與第一個(gè)頂點(diǎn)相連。 |
|||||||||
turtle.get_poly() |
返回最后記錄的多邊形。 |
3.命令詳解
3.1turtle.circle(radius,extent=None,steps=None)
描述:以給定半徑畫圓
參數(shù):
radius(半徑):半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓;
extent(弧度)(optional);
steps(optional)(做半徑為radius的圓的內(nèi)切正多邊形,多邊形邊數(shù)為steps)。
舉例:
circle(50)#整圓;
circle(50,steps=3)#三角形;
circle(120,180)#半圓
4.實(shí)例
1、太陽花
# coding=utf-8 import turtle import time # 同時(shí)設(shè)置pencolor=color1, fillcolor=color2 turtle.color("red", "yellow") turtle.begin_fill() for _ in range(50): turtle.forward(200) turtle.left(170) turtle.end_fill() turtle.mainloop()
2、五角星
# coding=utf-8 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', 40, 'normal')) turtle.mainloop()
3、時(shí)鐘程序
# coding=utf-8 import turtle from datetime import * # 抬起畫筆,向前運(yùn)動(dòng)一段距離放下 def Skip(step): turtle.penup() turtle.forward(step) turtle.pendown() def mkHand(name, length): # 注冊(cè)Turtle形狀,建立表針Turtle turtle.reset() Skip(-length * 0.1) # 開始記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的第一個(gè)頂點(diǎn)。 turtle.begin_poly() turtle.forward(length * 1.1) # 停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與第一個(gè)頂點(diǎn)相連。 turtle.end_poly() # 返回最后記錄的多邊形。 handForm = turtle.get_poly() turtle.register_shape(name, handForm) def Init(): global secHand, minHand, hurHand, printer # 重置Turtle指向北 turtle.mode("logo") # 建立三個(gè)表針Turtle并初始化 mkHand("secHand", 135) mkHand("minHand", 125) mkHand("hurHand", 90) secHand = turtle.Turtle() secHand.shape("secHand") minHand = turtle.Turtle() minHand.shape("minHand") hurHand = turtle.Turtle() hurHand.shape("hurHand") for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 3) hand.speed(0) # 建立輸出文字Turtle printer = turtle.Turtle() # 隱藏畫筆的turtle形狀 printer.hideturtle() printer.penup() def SetupClock(radius): # 建立表的外框 turtle.reset() turtle.pensize(7) for i in range(60): Skip(radius) if i % 5 == 0: turtle.forward(20) Skip(-radius - 20) Skip(radius + 20) if i == 0: turtle.write(int(12), align="center", font=("Courier", 14, "bold")) elif i == 30: Skip(25) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-25) elif (i == 25 or i == 35): Skip(20) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-20) else: turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-radius - 20) else: turtle.dot(5) Skip(-radius) turtle.right(6) def Week(t): week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] return week[t.weekday()] def Date(t): y = t.year m = t.month d = t.day return "%s %d%d" % (y, m, d) def Tick(): # 繪制表針的動(dòng)態(tài)顯示 t = datetime.today() second = t.second + t.microsecond * 0.000001 minute = t.minute + second / 60.0 hour = t.hour + minute / 60.0 secHand.setheading(6 * second) minHand.setheading(6 * minute) hurHand.setheading(30 * hour) turtle.tracer(False) printer.forward(65) printer.write(Week(t), align="center", font=("Courier", 14, "bold")) printer.back(130) printer.write(Date(t), align="center", font=("Courier", 14, "bold")) printer.home() turtle.tracer(True) # 100ms后繼續(xù)調(diào)用tick turtle.ontimer(Tick, 100) def main(): # 打開/關(guān)閉龜動(dòng)畫,并為更新圖紙?jiān)O(shè)置延遲。 turtle.tracer(False) Init() SetupClock(160) turtle.tracer(True) Tick() turtle.mainloop() if __name__ == "__main__": main()
畫一個(gè)邊長為60的正方形,并填充為紅色,邊框?yàn)樗{(lán)色
#-*- coding: utf-8 -*- import turtle turtle.reset() a= 60 turtle.fillcolor("red") turtle.pencolor("blue") turtle.pensize(10) turtle.fill(True) turtle.left(90) turtle.forward(a) turtle.left(90) turtle.forward(a) turtle.left(90) turtle.forward(a) turtle.left(90) turtle.forward(a) turtle.fill(False)
最后一個(gè)實(shí)例:
from numpy import * from random import random import turtle turtle.reset() x = array([[.5],[.5]]) p = [0.85,0.92,0.99,1.00] A1 = array([[.85, 0.04], [-0.04,.85]]) b1 = array([[0],[1.6]]) A2 = array([[0.20,-0.26], [0.23,0.22]]) b2 = array([[0],[1.6]]) A3 = array([[-0.15,0.28], [0.26,0.24]]) b3 = array([[0],[0.44]]) A4 = array([[0,0], [0,0.16]]) turtle.color("blue") cnt = 1 while True: cnt += 1 if cnt == 2000: break r = random() if r < p[0]: x = dot(A1 , x) + b1 elif r < p[1]: x = dot(A2 , x) + b2 elif r < p[2]: x = dot(A3 , x) + b3 else: x = dot(A4 , x) #print x[1] turtle.up() turtle.goto(x[0][0] * 50,x[1][0] * 40 - 240) turtle.down() turtle.dot()
總結(jié)
以上就是本文關(guān)于Python內(nèi)置模塊turtle繪圖詳解的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python urlencode和unquote函數(shù)使用實(shí)例解析
這篇文章主要介紹了Python urlencode和unquote函數(shù)使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03python筆記(1) 關(guān)于我們應(yīng)不應(yīng)該繼續(xù)學(xué)習(xí)python
關(guān)于Python,如果你要學(xué)習(xí),建議大家查看一下網(wǎng)站:因?yàn)楸救艘彩莿倓倹Q定收集點(diǎn)零碎時(shí)間來學(xué)習(xí)下它,推薦可能并不是最好的2012-10-10Python+Redis從零打造分布式鎖實(shí)戰(zhàn)示例
Redis作為一款高性能的內(nèi)存鍵值數(shù)據(jù)庫,憑借其支持原子操作、高并發(fā)和數(shù)據(jù)持久化等特性,非常適合用來實(shí)現(xiàn)分布式鎖,本文將詳細(xì)探討如何使用Python結(jié)合Redis從簡單到復(fù)雜地實(shí)現(xiàn)分布式鎖,并提供相應(yīng)的示例代碼2024-01-01pytorch如何自定義forward和backward函數(shù)
PyTorch自動(dòng)求導(dǎo)功能強(qiáng)大,但在特定情況下需要用戶自行定義backward函數(shù),通過實(shí)例解釋了保存變量、計(jì)算梯度、鏈?zhǔn)椒▌t等核心概念,并展示了如何通過自定義函數(shù)集成到網(wǎng)絡(luò)中以及如何正確返回梯度,此外,還討論了多輸出情況下的梯度傳遞2024-10-10Python數(shù)據(jù)處理的六種方式總結(jié)
在 Python 的數(shù)據(jù)處理方面經(jīng)常會(huì)用到一些比較常用的數(shù)據(jù)處理方式,比如pandas、numpy等等。今天介紹的這款 Python 數(shù)據(jù)處理的管道數(shù)據(jù)處理方式,通過鏈?zhǔn)胶瘮?shù)的方式可以輕松的完成對(duì)list列表數(shù)據(jù)的處理,希望對(duì)大家有所幫助2022-11-11Python報(bào)表自動(dòng)化之從數(shù)據(jù)到可視化一站式指南
在現(xiàn)代數(shù)據(jù)驅(qū)動(dòng)的世界中,生成清晰、有用的報(bào)表對(duì)于業(yè)務(wù)決策至關(guān)重要,Python作為一門強(qiáng)大的編程語言,提供了豐富的庫和工具,使得報(bào)表自動(dòng)化變得輕而易舉,本文將詳細(xì)介紹如何利用Python從數(shù)據(jù)處理到可視化,實(shí)現(xiàn)報(bào)表自動(dòng)化的全過程2024-01-01用Python進(jìn)行TCP網(wǎng)絡(luò)編程的教程
這篇文章主要介紹了用Python進(jìn)行TCP網(wǎng)絡(luò)編程的教程,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04