溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中turtle.done()的作用有哪些

發(fā)布時間:2021-02-04 09:46:34 來源:億速云 閱讀:864 作者:小新 欄目:編程語言

小編給大家分享一下python中turtle.done()的作用有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

在Python中,“turtle.done()”的作用是暫停程序,停止畫筆繪制,但繪圖窗體不關(guān)閉,直到用戶關(guān)閉Python Turtle圖形化窗口為止;它的目的是給用戶時間來查看圖形,沒有它,圖形窗口會在程序完成是立即關(guān)閉。

turtle.done()的作用:暫停程序,停止畫筆繪制,但繪圖窗體不關(guān)閉,直到用戶關(guān)閉Python Turtle圖形化窗口為止。

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

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

1. 畫布(canvas)

       畫布就是turtle為我們展開用于繪圖區(qū)域,我們可以設(shè)置它的大小和初始位置。

       設(shè)置畫布大小

        turtle.screensize(canvwidth=None, canvheight=None, bg=None),參數(shù)分別為畫布的寬(單位像素), 高, 背景顏色。

       如:turtle.screensize(800,600, "green")

              turtle.screensize() #返回默認大小(400, 300)

        turtle.setup(width=0.5, height=0.75, startx=None, starty=None),參數(shù):width, height: 輸入寬和高為整數(shù)時, 表示像素; 為小數(shù)時, 表示占據(jù)電腦屏幕的比例,(startx, starty): 這一坐標表示矩形窗口左上角頂點的位置, 如果為空,則窗口位于屏幕中心。

       如:turtle.setup(width=0.6,height=0.6)

              turtle.setup(width=800,height=800, startx=100, starty=100)

2. 畫筆

2.1 畫筆的狀態(tài)

       在畫布上,默認有一個坐標原點為畫布中心的坐標軸,坐標原點上有一只面朝x軸正方向小烏龜。這里我們描述小烏龜時使用了兩個詞語:坐標原點(位置),面朝x軸正方向(方向), turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態(tài)。

2.2 畫筆的屬性

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

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

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

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

2.3 繪圖命令

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

(1)    畫筆運動命令

命令

說明

turtle.forward(distance)

向當前畫筆方向移動distance像素長度

turtle.backward(distance)

向當前畫筆相反方向移動distance像素長度

turtle.right(degree)

順時針移動degree°

turtle.left(degree)

逆時針移動degree°

turtle.pendown()

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

turtle.goto(x,y)

將畫筆移動到坐標為x,y的位置

turtle.penup()

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

turtle.circle()

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

setx( )

將當前x軸移動到指定位置

sety( )

將當前y軸移動到指定位置

setheading(angle)

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

home()

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

dot(r)

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

(2)     畫筆控制命令

命令

說明

turtle.fillcolor(colorstring)

繪制圖形的填充顏色

turtle.color(color1, color2)

同時設(shè)置pencolor=color1, fillcolor=color2

turtle.filling()

返回當前是否在填充狀態(tài)

turtle.begin_fill()

準備開始填充圖形

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()

返回當前turtle是否可見

stamp()

復(fù)制當前圖形

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í)行重置。如果沒有給出模式,則返回當前模式。

模式初始龜標題正角度standard向右(東)逆時針logo向上(北)順時針

turtle.delay(delay=None)

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

turtle.begin_poly()

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

turtle.end_poly()

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

turtle.get_poly()

返回最后記錄的多邊形。

3. 命令詳解

       3.1 turtle.circle(radius, extent=None, steps=None)

       描述:以給定半徑畫圓

       參數(shù):

       radius(半徑):半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓;

       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()

python中turtle.done()的作用有哪些

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()

python中turtle.done()的作用有哪些

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)
    # 開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第一個頂點。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止記錄多邊形的頂點。當前的烏龜位置是多邊形的最后一個頂點。將與第一個頂點相連。
    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()

python中turtle.done()的作用有哪些

以上是“python中turtle.done()的作用有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI