What is the function of turtle.done()

青灯夜游
Release: 2021-02-03 15:53:18
Original
20266 people have browsed it

In Python, the function of "turtle.done()" is to pause the program and stop the brush drawing, but the drawing form will not close until the user closes the Python Turtle graphical window; its purpose is to give the user time to view the graph; without it, the graph window will close as soon as the program completes.

What is the function of turtle.done()

#The operating environment of this tutorial: Windows 7 system, Python 3 version, Dell G3 computer.

The role of turtle.done(): pause the program and stop brush drawing, but the drawing form will not close until the user closes the Python Turtle graphical window.

The Turtle library is a very popular function library for drawing images in the Python language. Imagine a little turtle, at the origin of a coordinate system with the horizontal axis as x and the vertical axis as y, (0,0 ) position, it moves in this plane coordinate system according to the control of a set of function instructions, thereby drawing graphics on the path it crawls.

Basic knowledge of turtle drawing:

1. Canvas

The canvas is the turtle that expands the drawing area for us. We can set its size and initial position.

Set the canvas size

turtle.screensize(canvwidth=None, canvheight=None, bg=None), the parameters are the width of the canvas (unit pixels), height, background color.

For example: turtle.screensize(800,600, "green")

## Turtle.screensize() #Return to the default size (400, 300)

turtle.setup(width=0.5, height=0.75, startx=None, starty=None), parameters: width, height: When the input width and height are integers, Represents pixels; when it is a decimal, it represents the proportion of the computer screen occupied, (startx, starty): This coordinate represents the position of the upper left corner of the rectangular window. If it is empty, the window is located at the center of the screen.

                                                                                                                                                                                                                                                                            . =800, startx=100, starty=100)

2. Brush

2.1 The status of the brush

On the canvas, there is a coordinate axis whose coordinate origin is the center of the canvas by default, and there is a surface on the coordinate origin The little turtle faces the positive direction of the x-axis. Here we use two words to describe the little turtle: coordinate origin (position), facing the positive direction of the x-axis (direction). In turtle drawing, the position and direction are used to describe the state of the little turtle (brush).

2.2 Brush properties

Brush (Brush properties, color, line drawing The width of the brush, etc.)

1) turtle.pensize(): Set the width of the brush;

2) turtle.pencolor(): None The parameters are passed in, and the current brush color is returned. The parameters passed in set the brush color, which can be strings such as "green", "red", or RGB 3-tuples.

3) turtle.speed(speed): Set the brush movement speed, the brush drawing speed range is [0,10] integer, the larger the number, the faster.

2.3 Drawing commands

There are many commands to control turtle drawing. These commands can It is divided into 3 types: one is the motion command, one is the brush control command, and the other is the global control command.

(1) Brush movement command

##Command##turtle.penup() Filed The pen moves without drawing graphics. It is used to draw in another place##turtle.circle()setx( )sety( )setheading(angle)home()##dot(r)(2) Brush control commands

Description

##turtle.forward(distance)

Move the distance pixel length toward the current brush direction

##turtle.backward(distance)

Move the distance pixel length in the opposite direction of the current brush

# #turtle.right(degree)

Move degree°clockwise

turtle.left(degree)

Move degree° counterclockwise

turtle.pendown()

Draw graphics when moving, and also draw by default

turtle.goto(x,y)

Move the brush to the coordinates of x,y Location

Draw a circle with a positive (negative) radius, which means the center of the circle is to the left (right) of the brush

Move the current x-axis to the specified position

Move the current y-axis to the specified position

Set the current heading to angle angle

Set the current brush position to the origin, facing east.

Draw a specified diameter and color dots


Commandturtle.fillcolor(colorstring)Display the turtle shape of the brush

(3) Global control command

Description

##The fill color of drawing graphics

turtle.color(color1, color2)

Also set pencolor=color1, fillcolor=color2

##turtle.filling()

Returns whether the current In the fill state

turtle.begin_fill()

Prepare to start filling the graphics

##turtle.end_fill()

Filling completed

turtle.hideturtle()

Hide the turtle shape of the brush

##turtle.showturtle()

##turtle.reset()Clear the window and reset the turtle state to the starting state turtle. undo()Undo the previous turtle action## turtle.isvisible()stamp() turtle.write(s [,font=("font-name",font_size,"font_type")])##( 4) Other commands

##Command

Description

##turtle.clear()

Clear the turtle window, but the turtle's position and status will not change

Returns whether the current turtle is visible

Copy the current graphic

Write text, s is the text content, font is the parameter of the font, which are the font name, size and type respectively; font is optional, and the font parameter is also optional

Command, Stop recording polygon vertices. The current turtle position is the last vertex of the polygon. Will be connected to the first vertex. Return the last recorded polygon.

3. Command details

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

Description: given Draw a circle with a fixed radius

Parameters:

Radius: The radius is positive (negative), which means the center of the circle is on the left (right) of the brush ) draw a circle;

extent(radians) (optional);

steps (optional) (make inscribed circles of radius radius Regular polygon, the number of sides of the polygon is steps).

Example:

circle(50) #Full circle;

circle(50, steps=3) #Triangle;

circle(120, 180) # Semicircle

Example:

1. Sunflower

# coding=utf-8
import turtle
import time

# 同时设置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()
Copy after login

2. Pentacle

# 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()
Copy after login


3. Clock program

# 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 = 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后继续调用tick
    turtle.ontimer(Tick, 100)
 
def main():
    # 打开/关闭龟动画,并为更新图纸设置延迟。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.mainloop()
 
if __name__ == "__main__":
    main()
Copy after login


For more knowledge about computer programming, please visit:

Programming Video! !

##Description

##turtle.mainloop() or turtle.done()

Start the event loop

-
Call the

mainloop function of Tkinter. # must be the last statement in the turtle graphics program.

##turtle.mode(mode=None)

Set turtle mode ( "standard"

"logo" or "world ”) and perform a reset. If no mode is given, returns the current mode. Mode initial turtle title positive angle standard right (east) counterclockwise logo upward (north) clockwise

turtle.delay(delay=None)

Set or return the drawing delay in milliseconds.

##turtle.begin_poly()

Start recording the vertices of the polygon. The current turtle position is the first vertex of the polygon.

##turtle.end_poly()

##turtle.get_poly()

The above is the detailed content of What is the function of turtle.done(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template