Python中turtle.write方法使用說明
turtle.write方法使用說明
關于turtle可參見 Python的turtle模塊:http://www.dhdzp.com/article/238830.htm
turtle.write()方法
在當前烏龜位置寫入文本。如:
turtle.write("你好啊", align="center",font=("宋體",10,"normal"))其中
你好啊 寫入Turtle繪畫屏幕的文字,是字符串格式,要有引號。
move(可選):在默認情況下,move為false。如果move為true,則筆將移動到右下角。
align(可選):可取值是left即左、center即中、right即右之一,是字符串格式。
font(可選):字體三元組(fontname、fontsize、fonttype),fontname即字體名稱,fontsize即字體大小,fonttype即字體類型如:normal、bold、italic。。
例子
import turtle
info = "你輸入的文字"
turtle.penup()
turtle.fd(-300)
turtle.pencolor('red')
for i in info:
turtle.write(i, font=('宋體',40,'normal'))
turtle.fd(60)
turtle.hideturtle()運行效果如下:

繪制一朵小花的例子
import turtle as t
t.penup()
t.fd(-200)
t.write("一朵小花\n", align="right", font=("楷體", 16, "bold"))
def draw_leaf():
for i in range(2):
for j in range(15):
t.forward(5)
t.right(6)
t.right(90)
t.goto(0,-150)
t.left(90)
t.down()
t.forward(50)
t.fillcolor("green")
t.begin_fill()
draw_leaf()
t.end_fill()
t.forward(50)
t.right(270)
t.fillcolor("green")
t.begin_fill()
draw_leaf()
t.end_fill()
t.right(90)
t.forward(130)
t.fillcolor("red")
t.begin_fill()
for i in range(6):
draw_leaf()
t.right(60)
t.end_fill()
t.done()運行效果如下:

如何使用turtle.write方法將文字顯示為一個圓圈?
可近似地將畫筆的運動軌跡看為一個正多邊形。
根據(jù)多邊形內角和公式:度數(shù)=(邊數(shù)-2)*180,
那么,每次旋轉的度數(shù)為:180-度數(shù)/角數(shù)=180-(邊數(shù)-2)*180/邊數(shù)。
易知,邊數(shù)=角數(shù)=文字數(shù)
所以每次旋轉的度數(shù)為:180-(文字數(shù)-2)*180/文字數(shù)=360/文字數(shù)。
例如
#將文字顯示為一個圓圈
import turtle
text="你要顯示的文字"
turtle.pu()
x=len(text)
for i in text:
turtle.write(i,font='consolas')
turtle.rt(360/x)
turtle.pu()
turtle.fd(30)
turtle.hideturtle()運行效果如下:

總結
到此這篇關于Python中turtle.write方法使用說明的文章就介紹到這了,更多相關Python turtle.write方法使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
pytorch教程網(wǎng)絡和損失函數(shù)的可視化代碼示例
這篇文章主要介紹了pytorch教程中網(wǎng)絡和損失函數(shù)的可視化,文中附含詳細的代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
python 利用jieba.analyse進行 關鍵詞提取
這篇文章主要介紹了python 利用jieba.analyse進行關鍵詞提取的方法,幫助大家更好的利用python,感興趣的朋友可以了解下2020-12-12
Pytorch中的backward()多個loss函數(shù)用法
這篇文章主要介紹了Pytorch中的backward()多個loss函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05

