亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Python繪圖Turtle庫

 更新時間:2019年10月12日 11:57:09   作者:chen~先生  
Turtle庫是Python語言中一個很流行的繪制圖像的函數(shù)庫,這篇文章主要介紹了Python繪圖Turtle庫的相關(guān)知識,需要的朋友可以參考下

 Turtle庫是Python語言中一個很流行的繪制圖像的函數(shù)庫,想象一個小烏龜,在一個橫軸為x、縱軸為y的坐標(biāo)系原點,(0,0)位置開始,它根據(jù)一組函數(shù)指令的控制,在這個平面坐標(biāo)系中移動,從而在它爬行的路徑上繪制了圖形。

turtle繪圖的基礎(chǔ)知識:

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ù)時, 表示占據(jù)電腦屏幕的比例,(startx, starty): 這一坐標(biāo)表示矩形窗口左上角頂點的位置, 如果為空,則窗口位于屏幕中心。

        如: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)有一個坐標(biāo)原點為畫布中心的坐標(biāo)軸,坐標(biāo)原點上有一只面朝x軸正方向小烏龜。這里我們描述小烏龜時使用了兩個詞語:坐標(biāo)原點(位置),面朝x軸正方向(方向), turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態(tài)。

2.2 畫筆的屬性

        畫筆(畫筆的屬性,顏色、畫線的寬度等)

        1) turtle.pensize():設(shè)置畫筆的寬度;

        2) turtle.pencolor():沒有參數(shù)傳入,返回當(dāng)前畫筆顏色,傳入?yún)?shù)設(shè)置畫筆顏色,可以是字符串如"green", "red",也可以是RGB 3元組。

        3) turtle.speed(speed):設(shè)置畫筆移動速度,畫筆繪制的速度范圍[0,10]整數(shù),數(shù)字越大越快。

2.3 繪圖命令

         操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:一種為運動命令,一種為畫筆控制命令,還有一種是全局控制命令。

(1)    畫筆運動命令

命令

說明

turtle.forward(distance)

向當(dāng)前畫筆方向移動distance像素長度

turtle.backward(distance)

向當(dāng)前畫筆相反方向移動distance像素長度

turtle.right(degree)

順時針移動degree°

turtle.left(degree)

逆時針移動degree°

turtle.pendown()

移動時繪制圖形,缺省時也為繪制

turtle.goto(x,y)

將畫筆移動到坐標(biāo)為x,y的位置

turtle.penup()

提起筆移動,不繪制圖形,用于另起一個地方繪制

turtle.circle()

畫圓,半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓

setx( )

將當(dāng)前x軸移動到指定位置

sety( )

將當(dāng)前y軸移動到指定位置

setheading(angle)

設(shè)置當(dāng)前朝向為angle角度

home()

設(shè)置當(dāng)前畫筆位置為原點,朝向東。

dot(r)

繪制一個指定直徑和顏色的圓點

(2)     畫筆控制命令

命令

說明

turtle.fillcolor(colorstring)

繪制圖形的填充顏色

turtle.color(color1, color2)

同時設(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)不會改變

turtle.reset()

清空窗口,重置turtle狀態(tài)為起始狀態(tài)

turtle.undo()

撤銷上一個turtle動作

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為可選項,font參數(shù)也是可選項

(4)    其他命令

命令

說明

turtle.mainloop()或turtle.done()

啟動事件循環(huán) -調(diào)用Tkinter的mainloop函數(shù)。

必須是烏龜圖形程序中的最后一個語句。

turtle.mode(mode=None)

設(shè)置烏龜模式(“standard”,“l(fā)ogo”或“world”)并執(zhí)行重置。如果沒有給出模式,則返回當(dāng)前模式。

模式

初始龜標(biāo)題

正角度

standard

向右(東)

逆時針

logo

向上(北)

順時針

turtle.delay(delay=None)

設(shè)置或返回以毫秒為單位的繪圖延遲。

turtle.begin_poly()

開始記錄多邊形的頂點。當(dāng)前的烏龜位置是多邊形的第一個頂點。

turtle.end_poly()

停止記錄多邊形的頂點。當(dāng)前的烏龜位置是多邊形的最后一個頂點。將與第一個頂點相連。

turtle.get_poly()

返回最后記錄的多邊形。

3. 命令詳解

        3.1 turtle.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) # 半圓

實例:

1、太陽花

# coding=utf-8
import turtle
import time
# 同時設(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、時鐘程序

# coding=utf-8
import turtle
from datetime import *
# 抬起畫筆,向前運動一段距離放下
def Skip(step):
 turtle.penup()
 turtle.forward(step)
 turtle.pendown()
def mkHand(name, length):
 # 注冊Turtle形狀,建立表針Turtle
 turtle.reset()
 Skip(-length * 0.1)
 # 開始記錄多邊形的頂點。當(dāng)前的烏龜位置是多邊形的第一個頂點。
 turtle.begin_poly()
 turtle.forward(length * 1.1)
 # 停止記錄多邊形的頂點。當(dāng)前的烏龜位置是多邊形的最后一個頂點。將與第一個頂點相連。
 turtle.end_poly()
 # 返回最后記錄的多邊形。
 handForm = turtle.get_poly()
 turtle.register_shape(name, handForm)
def Init():
 global secHand, minHand, hurHand, printer
 # 重置Turtle指向北
 turtle.mode("logo")
 # 建立三個表針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():
 # 繪制表針的動態(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)閉龜動畫,并為更新圖紙設(shè)置延遲。
 turtle.tracer(False)
 Init()
 SetupClock(160)
 turtle.tracer(True)
 Tick()
 turtle.mainloop()
if __name__ == "__main__":
 main()

總結(jié)

以上所述是小編給大家介紹的Python繪圖Turtle庫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!  

相關(guān)文章

最新評論