最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 用turtle實現(xiàn)用正方形畫圓的例子

 更新時間:2019年11月21日 10:15:32   作者:春江暮客  
今天小編就為大家分享一篇Python 用turtle實現(xiàn)用正方形畫圓的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近發(fā)現(xiàn)一個很有意思的畫圖的python庫,叫做turtle,這里先說下用turtle這個庫來實現(xiàn)用正方形畫圓的思路。

每次都用烏龜(turtle)來畫出一個正方形,然后通過旋轉(zhuǎn)3°后,繼續(xù)畫一樣的正方形,在通過120次循環(huán)后就實現(xiàn)了完整的圓,這里當(dāng)然也可以用其他的角度和次數(shù),只要能完成360度就可以了。

先看完成的圖形和代碼。

代碼如下:

import turtle

window = turtle.Screen() #設(shè)置好畫圖的基本參數(shù)
window.bgcolor(“blue”)
wugui= turtle.Turtle()
wugui.shape(“turtle”)
wugui.color(“red”)
wugui.speed(5)
for i in range(120): #這里設(shè)定正方形的個數(shù)
 wugui.forward(100)
 wuguiright(90)
 wugui.forward(100)
 wugui.right(90)
 wugui.forward(100)
 wugui.right(90)
 wugui.forward(100)
 wugui.right(93)#這里決定每次旋轉(zhuǎn)角度,也就決定了需要畫正方形的次數(shù)。

window.exitonclick()

代碼應(yīng)該很簡單易懂,就不再說了。turtle真的是非常強大的一個繪圖工具,可以繪制各種各樣有趣的圖形,詳情請看 turtle官方文檔,這里說點基本的參數(shù)與用法吧。主要包括兩部分,烏龜與畫布。

烏龜方法

烏龜運動

烏龜移動與繪畫

forward() | fd() 向前移動指定的距離。參數(shù):(integer or float))一個數(shù)字

backward() | bk() | back() 向后移動指定的距離。參數(shù):(integer or float))一個數(shù)字

right() | rt() left() | lt() 向右 旋轉(zhuǎn)指定的角度。參數(shù):(integer or float))一個數(shù)字

goto() | setpos() | setposition() 去到位置(x,y)。參數(shù):(x, y=None))一個數(shù)字

setx() 設(shè)置X位置。參數(shù):(integer or float)一個數(shù)字

sety() 設(shè)置Y位置。參數(shù):(integer or float)一個數(shù)字

setheading() | seth() 方向設(shè)置為to_angle.就是東西南北方向,上北下南左西右東

home() 移動到原點 – 坐標(biāo)(0,0):并將其標(biāo)題設(shè)置為其起始方向

circle() 繪制一個給定半徑的圓。參數(shù):(radius,extent,steps)(一個數(shù)字__半徑,如果值為正則逆時針,負(fù)數(shù)為順時針__,一個數(shù)字, 執(zhí)行的步數(shù))

dot() 用顏色畫出一個直徑大小的圓點。參數(shù):(size,color)(一個大于1的整數(shù)_可None,顏色值)

stamp() 將當(dāng)前位置上的形狀復(fù)制到畫布上,返回stamp_id.可通過下方的clearstamp刪除

clearstamp() 刪除stamp()返回來的值,參數(shù):(stamp_id)stamp函數(shù)返回值

clearstamps() 刪除所有的stamp,默認(rèn)無參數(shù),刪除所有

undo() 撤銷上一步動作

speed() 烏龜爬行速度,我們這設(shè)置的是5,不設(shè)置為最快,直接生成

烏龜當(dāng)前狀態(tài)

position() | pos() 當(dāng)前位置
towards() 返回與指定點之間的角度 參數(shù):(X,Y)一個位置
xcor() 返回烏龜X坐標(biāo)
ycor() 返回烏龜Y坐標(biāo)
heading() 返回當(dāng)前烏龜?shù)姆较蛑?br /> distance() 返回烏龜與坐標(biāo)點之間的距離。參數(shù):(X,Y)一個位置

設(shè)置與測量

degrees() 設(shè)置整個圓的角度,最好不要動。參數(shù):(integer or float)一個整數(shù)
radians() 將角度測量單位設(shè)置為弧度。360度就是2π

畫筆控制

繪畫狀態(tài)

pendown() | pd() | down() 將筆落下放在圖上,移動的時候?qū)L圖
penup() | pu() | up() 將筆提起來,移動的時候?qū)⒉粫L圖
pensize() | width() 設(shè)置線條的粗細(xì)。參數(shù):(width)一個正數(shù)

pen() 使用鍵值對設(shè)置筆的屬性

“shown”: True/False 顯示
“pendown”: True/False 筆落下
“pencolor”: color-string or color-tuple 筆的顏色
“fillcolor”: color-string or color-tuple 填充顏色
“pensize”: positive number      筆大?。ㄕ麛?shù))
“speed”: number in range 0..10    繪畫速度(范圍0-10)
“resizemode”: “auto” or “user” or “noresize” 大小調(diào)整模式
“stretchfactor”: (positive number, positive number) 拉伸參數(shù)
“outline”: positive number 外部
“tilt”: number 傾斜

isdown() 如果筆停止返回True,反之返回False

顏色控制

color() 顏色,直接使用返回當(dāng)前筆顏色與填充顏色
pencolor() 設(shè)置筆的顏色
fillcolor() 設(shè)置筆的填充顏色

填充

filling() 返回填充狀態(tài),
begin_fill() 在填充之前使用
end_fill() 結(jié)束填充

更多繪畫控制

reset() 重置所有參數(shù)
clear() 刪除繪畫,與reset不同之處僅僅是刪除圖形,參數(shù)保留
write() 寫文字

arg – object to be written to the TurtleScreen 寫到TurtleScreen的參數(shù)
move – True/False  移動
align – one of the strings “l(fā)eft”, “center” or right” 對齊參數(shù)3選1(left,right,center)
font – a triple (fontname, fontsize, fonttype) 字體

烏龜狀態(tài)

可視性

showturtle() | st() 顯示烏龜?shù)男螤?br /> hideturtle() | ht() 隱藏烏龜?shù)男螤?br /> isvisible() 是否可見,返回True or False

外表

shape() 設(shè)置烏龜?shù)膱D形形狀,可選( “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”)

resizemode() 大小調(diào)整模式

“auto”: adapts the appearance of the turtle corresponding to the value of pensize.   由畫筆大小決定(自動)
“user”: adapts the appearance of the turtle according to the values of stretchfactor and outlinewidth (outline), 由拉伸參數(shù)決定
“noresize”: no adaption of the turtle's appearance takes place.            不調(diào)整

shapesize() | turtlesize() 返回筆的屬性。
shearfactor() 設(shè)置或者返回當(dāng)前剪切因子
settiltangle() 與tilt() 一樣,只是可以為空,則返回當(dāng)前旋轉(zhuǎn)角度
tiltangle() 棄用
tilt() 設(shè)置當(dāng)前烏龜角度,不調(diào)整烏龜前進方向(僅僅改變?yōu)觚敇幼樱?br /> shapetransform() 設(shè)置或返回烏龜?shù)男螤畹漠?dāng)前轉(zhuǎn)換矩陣
get_shapepoly() 返回當(dāng)前形狀的坐標(biāo)

監(jiān)聽動作

onclick() 鼠標(biāo)點擊事件

fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas函數(shù)需要有兩個參數(shù)
num – number of the mouse-button, defaults to 1 (left mouse button) 單擊次數(shù),默認(rèn)1
add – True or False – if True, a new binding will be added, otherwise it will replace a former binding 添加新的綁定函數(shù),否則替代之前函數(shù)
例子:def turn(x, y):
  。。。 left(180)
onclick(turn)

onrelease() 鼠標(biāo)釋放事件,同上

ondrag() 鼠標(biāo)移動事件,同上

烏龜一些特殊方法

begin_poly() 開始記錄多邊形的頂點,當(dāng)前點為起始點
end_poly() 結(jié)束記錄多邊形的頂點,當(dāng)前點為起始點
get_poly() 返回最后記錄的多邊形
clone() 復(fù)制一個一模一樣的烏龜
getturtle() | getpen() 獲取trutle對象本身
getscreen() 獲取畫布對象
setundobuffer() 設(shè)置或禁用中斷器
undobufferentries() 返回undobuffer中的條目數(shù)

畫布的方法

窗口控制

bgcolor() 設(shè)置或返回當(dāng)前畫布的背景顏色
bgpic() 設(shè)置或返回當(dāng)前畫布的背景圖片名稱
clear() | clearscreen() 清除圖形
reset() | resetscreen() 重置畫布

screensize() 畫布大小

canvwidth – positive integer, new width of canvas in pixels 寬度
canvheight – positive integer, new height of canvas in pixels 高度
bg – colorstring or color-tuple, new background color     顏色

setworldcoordinates() 全局坐標(biāo)

llx – a number, x-coordinate of lower left corner of canvas   左下X坐標(biāo)
lly – a number, y-coordinate of lower left corner of canvas   左下X坐標(biāo)
urx – a number, x-coordinate of upper right corner of canvas  右下X坐標(biāo)
ury – a number, y-coordinate of upper right corner of canvas  右下X坐標(biāo)

動畫控制

delay() 動畫延遲(毫秒)參數(shù):(integer )一個數(shù)字
tracer() 開啟動畫,設(shè)置延遲

n – nonnegative integer    n個動作執(zhí)行一次
delay – nonnegative integer  延遲,毫秒

update() 更新畫布,當(dāng)tracer關(guān)閉時使用

畫布監(jiān)聽

listen() 開啟監(jiān)聽,將鼠標(biāo)定位到畫布
onkey() | onkeyrelease() 鍵盤彈起(需要位于焦點上,使用上面listen后)

fun – a function with no arguments or None 動作函數(shù)
key – a string: key (e.g. “a”) or key-symbol (e.g. “space”) 按鍵
onkeypress() 鍵盤按下事件,同上

onclick() | onscreenclick() 鼠標(biāo)點擊事件

fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas 函數(shù)需要兩個參數(shù)
num – number of the mouse-button, defaults to 1 (left mouse button) 點擊次數(shù)
add – True or False – if True, a new binding will be added, otherwise it will replace a former binding 是否是添加,還是替換
ontimer() 計時器

fun – a function with no arguments 無需函數(shù)
t – a number >= 0 事件間隔
mainloop() | done() 開始事件循環(huán),必須是烏龜繪畫中的最后一個函數(shù)

設(shè)置與特殊方法

mode() 繪圖模式,3選1 “standard”, “l(fā)ogo” or “world”
colormode() 顏色模式, 1.0 或者 255
getcanvas() 返回當(dāng)前TurtleScreen.的Canvas
getshapes() 返回當(dāng)前可用形狀
register_shape() | addshape() 3種調(diào)用方式。


1.直接調(diào)用圖片。screen.register_shape(“turtle.gif”)

2.調(diào)用形狀,制定點位置。

screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))

3,調(diào)用形狀,名字隨便取

turtles() 返回烏龜list數(shù)組
window_height() 返回窗口高度
window_width() 返回窗口寬度

輸入方法

textinput() 文字輸入

title – string 輸入名字
prompt – string 輸入的文本
numinput() 數(shù)字輸入

title – string 輸入名字
prompt – string 輸入文本
default – number (optional) 默認(rèn)
minval – number (optional) 最小
maxval – number (optional) 最大

屏幕特有方法

bye() 關(guān)閉turtle窗口
exitonclick() 鼠標(biāo)點擊關(guān)閉窗口
setup() 設(shè)置主窗口參數(shù)

width – if an integer, a size in pixels, if a float, a fraction of the screen; default is 50% of screen 寬 度
height – if an integer, the height in pixels, if a float, a fraction of the screen; default is 75% of screen 高度
startx – if positive, starting position in pixels from the left edge of the screen, if negative from the right edge, if None, center window horizontally 左邊開始位置
startx – if positive, starting position in pixels from the top edge of the screen, if negative from the bottom edge, if None, center window vertically 右邊開始位置
title() 設(shè)置繪畫窗口標(biāo)題

以上這篇Python 用turtle實現(xiàn)用正方形畫圓的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python多元非線性回歸及繪圖的實現(xiàn)

    Python多元非線性回歸及繪圖的實現(xiàn)

    本文主要介紹了Python多元非線性回歸及繪圖的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • Python自動提取項目中導(dǎo)入的庫及其版本信息

    Python自動提取項目中導(dǎo)入的庫及其版本信息

    在我們有時需要遷移或部署項目時,需要知道項目所依賴的三方包和版本,本文就來介紹一下Python自動提取項目中導(dǎo)入的庫及其版本信息,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Python編程中的反模式實例分析

    Python編程中的反模式實例分析

    這篇文章主要介紹了Python編程中的反模式,詳細(xì)講述了反模式的害處并以實例形式具體分析了容易造成的易錯點,對于Python學(xué)習(xí)來說具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • 在Django的模板中使用認(rèn)證數(shù)據(jù)的方法

    在Django的模板中使用認(rèn)證數(shù)據(jù)的方法

    這篇文章主要介紹了在Django的模板中使用認(rèn)證數(shù)據(jù)的方法,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下
    2015-07-07
  • Python模擬百度自動輸入搜索功能的實例

    Python模擬百度自動輸入搜索功能的實例

    今天小編就為大家分享一篇Python模擬百度自動輸入搜索功能的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Django-migrate報錯問題解決方案

    Django-migrate報錯問題解決方案

    這篇文章主要介紹了Django-migrate報錯問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • python使用htmllib分析網(wǎng)頁內(nèi)容的方法

    python使用htmllib分析網(wǎng)頁內(nèi)容的方法

    這篇文章主要介紹了python使用htmllib分析網(wǎng)頁內(nèi)容的方法,涉及Python使用htmllib模塊的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 詳解Python做一個名片管理系統(tǒng)

    詳解Python做一個名片管理系統(tǒng)

    這篇文章主要介紹了Python如何做一個名片管理系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python3 shelve模塊的詳解

    python3 shelve模塊的詳解

    這篇文章主要介紹了python3 shelve模塊的詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 利用python批量檢查網(wǎng)站的可用性

    利用python批量檢查網(wǎng)站的可用性

    當(dāng)大家的站點越來越來越多的時候會發(fā)現(xiàn)管理起來也挺復(fù)雜的,所以這篇文章給大家分享下利用python批量檢查網(wǎng)站的可用性的功能,對大家管理網(wǎng)站具有很實用的價值,有需要的朋友可以參考借鑒。
    2016-09-09

最新評論

乐业县| 出国| 定兴县| 许昌县| 镇沅| 渑池县| 白河县| 太湖县| 通渭县| 北海市| 邵东县| 阿巴嘎旗| 资源县| 怀柔区| 冀州市| 读书| 平陆县| 闻喜县| 临城县| 无棣县| 彭阳县| 绥化市| 阜南县| 太仆寺旗| 乐清市| 乌兰察布市| 灌阳县| 建宁县| 承德县| 灵石县| 比如县| 高青县| 静安区| 杂多县| 离岛区| 海兴县| 永兴县| 高邮市| 白水县| 定远县| 盐津县|