turtle
--- turtle graphics
(Click to view the official documentation.)
Brief introduction
Import Turtle # call the library. Turtle instance of an object, the default name for the turtle. turtle.forward (10 ) from turtle Import * # when you call the method, the object can be written turtle omitted.
The reason is called turtle, in fact, an imaginary, arrow drawing area as a pen, using it to draw graphics.
turtle module uses the tkinter
basic graphical interface, requiring the support of Tk Python version installed.
-m Tkinter python3 # If you have this module will pop up a small window.
and turtle module supports object-oriented processes, interfaces 2 + 2 classes:
- TurtleScreen class: the graphics window defined as the drawing property.
- Screen derived subclasses the object to produce a single embodiment.
- RawTurtle (also called RawPen) class: defines how the drawing.
- Derived subclasses Turtle (also called Pen): drawing on the object class instance Screen.
TurtleScreen above / Screen, the presence RawTurtle / Turtle all methods corresponding to the function, i.e., as part of the process-oriented interfaces.
Interface provides a process Screen
and Turtle
methods of the class corresponding to the function. The same name and function name corresponding method.
- Screen object is automatically created when a method corresponding to the Screen class function is called.
- Automatically creates an (anonymous) Turtle Turtle class of objects when the corresponding function is called a method.
⚠️ official documents in great detail. Also includes a large demo script.
example
Example, draw five-pointed star, five.
from turtle import * def draw(x, y): penUp () # pen and paper separately, not moving painting. goto (x, y) pendown () setheading(0) for i in range(5): forward(40) right(144) for x in range(0, 250, 50): #range(start, stop, step) draw(x, 0)