轉載自https://blog.csdn.net/wumenglu1018/article/details/78184930
在Python中有很多編寫圖形程序的方法,一個簡單的啟動圖形化程序設計的方法是使用Python內嵌的Turtle模塊。Turtle是Python內嵌的繪制線、圓以及其他形狀(包括文本)的圖形模塊。它很容易學習並且使用簡單。
一個Turtle實際上是一個對象,在導入Turtle模塊時,就創建了對象,然后,可以調用Turtle對象的各種方法完成不同的操作。
當創建一個Turtle對象時,它的位置被設定在(0,0)處——窗口的中心,而且它的方向被設置為向右。Turtle模塊用筆來繪制圖形。默認情況下,筆是向下的(就像真實的筆尖觸碰着一張紙)。如果筆是向下的,那么當移動Turtle的時候,它就會繪制出一條從當前位置到新位置的線。
下面兩個表是控制筆的繪制狀態的方法和移動Turtle的方法:
circle方法有三個參數:radius是必需的,extent和step是可有可無的。extent是一個角度,它決定繪制圓的哪一部分。step決定使用的階數。如果step是3/4/5/6……,那么circle方法將繪制一個里面包含被圓括住的的三邊、四邊、五邊、六邊或更多邊形(即正三角形、正方形、五邊形、六邊形等)。如果不指定階數,那么circle方法就只畫一個圓。
下面表是Turtle筆的顏色、填充和繪制方法:
代碼和執行實例:
(如果我理解有錯誤,希望大佬們一定要指出來,不要誤導了其他人才好)
附上面顯示的彩色圖形的完整代碼:
import turtle turtle.pensize(3) turtle.penup() turtle.goto(-100, -50) turtle.pendown() turtle.begin_fill() turtle.color("red") turtle.circle(40, steps=3) turtle.end_fill() turtle.penup() turtle.goto(0, -50) turtle.pendown() turtle.begin_fill() turtle.color("yellow") turtle.circle(50) turtle.end_fill() turtle.penup() turtle.goto(100, -50) turtle.pendown() turtle.begin_fill() turtle.fillcolor("green") turtle.circle(40, steps=6) turtle.end_fill() turtle.penup() turtle.goto(-50, 100) turtle.pendown() turtle.color("blue") turtle.write("Colorful Shapes", font = ("Times", 18,"bold")) turtle.end_fill() turtle.hideturtle() turtle.done()
補充:
- import turtle:導入Turtle模塊中定義的所有函數,這樣就可以使用所有函數。
- penup()和pendown():設置抬起或放下筆以控制移動筆時是否繪制。剛學習時可能對這兩個函數有點暈,我就這樣理解:penup,抬起筆,就是我抬起筆來做動作,這時候筆沒有挨着紙,所以之后移動時並不會畫圖形;pendown,落下筆,就是我現在將筆挨着紙了,一旦移動一步就會有個畫出來的形狀。
- forward():向箭頭指的方向畫形狀
- write():寫一個文本字符串
- Turtle的done()命令可以導致程序暫停直到用戶關閉Python Turtle圖形化窗口,它的目的是給用戶時間來查看圖形。沒有這一行,圖形窗口會在程序完成時立即關閉。
2017/10/9 20:45更新
先上代碼:
<span style="font-size:18px;"># -*- coding: utf-8 -*- """ 繪制帶顏色和文字的圖形 """ import turtle turtle.penup() turtle.goto(-200, -50) turtle.pendown() turtle.begin_fill() #開始填充 turtle.color("black") #填充黑色 turtle.circle(40) turtle.end_fill() #填充結束 turtle.color("red") #畫筆顏色為紅色 turtle.penup() turtle.goto(-100, -50) turtle.pendown() turtle.circle(40, steps=3) turtle.color("purple") #畫筆顏色為紫色 turtle.penup() turtle.goto(0, -50) turtle.pendown() turtle.begin_fill() #開始填充 turtle.fillcolor("gray") #填充灰色 turtle.circle(40, steps=4) turtle.end_fill() #填充結束 turtle.penup() turtle.goto(100, -50) turtle.pendown() turtle.begin_fill() #開始填充 turtle.fillcolor("yellow") #fillcolor為黃色 turtle.color("purple") #color為紫色 turtle.circle(40, steps=5) turtle.end_fill() #填充結束 turtle.penup() turtle.goto(200, -50) turtle.pendown() turtle.begin_fill() #開始填充 turtle.color("yellow") #color為黃色 turtle.fillcolor("green") #fillcolor為綠色 turtle.circle(40, steps=6) turtle.end_fill() #填充結束 turtle.color("blue") turtle.penup() turtle.goto(-50, 100) turtle.pendown() turtle.write("Colorful Shapes", font = ("Times", 18, "bold")) #隱藏箭頭 turtle.hideturtle() #暫停界面,使得用戶能夠看見展示的圖形 turtle.done()</span>
執行結果:
- turtle.begin_fill():在填充圖形前訪問這個方法
- turtle.end_fill():在最后調用begin_fill之前填充繪制的圖形
- turtle.color(c):設置筆的顏色
- turtle.fillcolor(c):設置筆填充顏色
所以:
在begin_fill()和end_fill()之間設置的turtle.color(c)和turtle.fillcolor(c)都可以作為填充色,例如黑色的圓。
將turtle.color(c)提到前面,不寫begin_fill()和end_fill(),則就不會實現填充,例如紅邊的三角形。
將turtle.color(c)提到前面,並在begin_fill()和end_fill()之間設置turtle.fillcolor(c),那么圖形的邊框和內部填充顏色分別對應於color和fillcolor,例如紫邊框灰色四邊形。
在begin_fill()和end_fill()之間設置不同的turtle.color(c)和turtle.fillcolor(c),則以排序在后面的為准去填充圖形,例如紫色五邊形和黃邊框綠色六邊形。五邊形的代碼是先fillcolor黃色,然后color紫色覆蓋,所以最終填充為紫色。六邊形是先color黃色,這樣就應該默認用黃色去填充,結果后面接着fillcolor綠色,所以最終填充為綠色。
所以,我上面的猜想不是很正確,剛處於學習階段,有些思考還不到位~~~~~~