python简单代码绘图-Python笔记:简单的绘图⼯具–turtle
上图是?Turtle官⽅⽂档 中给出的例⼦,代码为:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
1
2
3
4
5
6
7
8
9
10
fromturtleimport*
color('red','yellow')
begin_fill()
whileTrue:
forward(200)
left(170)
ifabs(pos())<1:
break
end_fill()
done()
下⾯我们简单做个关于Turtle的介绍。
在Python中有很多编写图形程序的⽅法,⼀个简单的启动图形化程序设计的⽅法是使⽤Python内嵌的Turtle模块。Turtle是Python内嵌的绘制线、圆以及其他形状(包括⽂本)的图形模块。它很容易学习并且使⽤简单。
⼀个Turtle实际上是⼀个对象,在导⼊Turtle模块时,就创建了对象,然后,可以调⽤Turtle对象的各种⽅法完成不同的操作。
当创建⼀个Turtle对象时,它的位置被设定在(0,0)处——窗⼝的中⼼,⽽且它的⽅向被设置为向右。Turtle模块⽤笔来绘制图形。默认情况下,笔是向下的(就像真实的笔尖触碰着⼀张纸)。如果笔是向下的,那么当移动Turtle的时候,它就会绘制出⼀条从当前位置到新位置的线。
下⾯两个表是控制笔的绘制状态的⽅法和移动Turtle的⽅法:
import turtle
# Table 1:Turtle Pen Drawing State Methods
# Method Description
turtle.pendown() # Pulls the pen down -- drawing when moving.
turtle.penup() # Pulls the pen up -- no drawing when moving.
turtle.pensize(width) # Sets the line thickness to the specified width.
# Table 2:Turtle Mothon Methods
# Method Description
turtle.forward(d) # Moves the turtle forward by the specified distance in the direction the turtle is headed.
turtle.backward(d) # Moves the turtle backward by the specified distance in the opposite direction the turtle is headed. The turtle's direction is not changed.
turtle.right(angle) # Turns the turtle right by the specified angle.
turtle.left(angle) # Turns the turtle left by the specified angle.
<(x,y) # Moves the turtle to an absolute position.
turtle.setx(x) # Moves the turtle's x-coordinate to the specified position.
turtle.sety(y) # Moves the turtle's y-coordinate to the specified position.
turtle.setheading(angle) # Sets the orientation of the turtle to a specified angle. 0-East, 90-North, 180-West, 270-South.
turtle.home() # Moves the turtle to the origin (0,0) and east direction.
turtle.circle(r, ext, step) # Draws a circle with the specified radius, extent, and step.
turtle.dot(diameter, color) # Draws a circle with the specified diameter and color.
turtle.undo() # Undo(repeatedly) the last turtle action(s).
turtle.speed(s) # Sets the turtle's speed to an integer between 1 and 10, with 10 being the fastest.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
python新手代码画图15
16
17
18
19
20
21
22
23
importturtle
# Table 1:Turtle Pen Drawing State Methods
# Method Description
turtle.pendown()# Pulls the pen down -- drawing when moving.
turtle.penup()# Pulls the pen up -- no drawing when moving.
turtle.pensize(width)# Sets the line thickness to the specified width.
# Table 2:Turtle Mothon Methods
# Method Description
turtle.forward(d)# Moves the turtle forward by the specified distance in the direction the turtle is headed.
turtle.backward(d)# Moves the turtle backward by the specified distance in the opposite direction the turtle is headed. The turtle's direction is not changed.
turtle.right(angle)# Turns the turtle right by the specified angle.
turtle.left(angle)# Turns the turtle left by the specified angle.
<(x,y)# Moves the turtle to an absolute position.
turtle.setx(x)# Moves the turtle's x-coordinate to the specified position.
turtle.sety(y)# Moves the turtle's y-coordinate to the specified position.
turtle.setheading(angle)# Sets the orientation of the turtle to a specified angle. 0-East, 90-North, 180-West, 270-South.
turtle.home()# Moves the turtle to the origin (0,0) and east direction.
turtle.circle(r,ext,step)# Draws a circle with the specified radius, extent, and step.
turtle.dot(diameter,color)# Draws a circle with the specified diameter and color.
turtle.undo()# Undo(repeatedly) the last turtle action(s).
turtle.speed(s)# Sets the turtle's speed to an integer between 1 and 10, with 10 being the fastest.
circle⽅法有三个参数:radius是必需的,extent和step是可有可⽆的。extent是⼀个⾓度,它决定绘制圆的哪⼀部分。step决定使⽤的阶数。如果step是3/4/5/6……,那么circle⽅法将绘制⼀个⾥⾯包含被圆括住的的三边、四边、五边、六边或更多边形(即正三⾓形、正⽅形、五边形、六边形等)。如果不指定阶数,那么circle⽅法就只画⼀个圆。
下⾯表是Turtle笔的颜⾊、填充和绘制⽅法:
# Table 3: Turtle Pen Color, Filling, and Drawing Methods
# Method Description
turtle.fillcolor(c) # Stes the pen fill color.
turtle.begin_fill() # Calls this method before filling a shape.
turtle.filling() # Returns the fill state: True if filling, False if not filling.
function.clear() # Clears the window. The state and the position to the original default value.
turtle.screensize(w, h) # Sets the width and height of the canvas.
turtle.hideturtle() # Makes the turtle invisible.
turtle.showturtle() # Makes the turtle visible.
turtle.isbisible() # Returns the turtle visible.
turtle.write(s, font=("Arial", 8, "normal"))# Writes the string s on the turtle position, Font is a triple consisting of fontname, fontsize, and fonttype.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Table 3: Turtle Pen Color, Filling, and Drawing Methods
# Method Description
turtle.fillcolor(c)# Stes the pen fill color.
turtle.begin_fill()# Calls this method before filling a shape.
turtle.filling()# Returns the fill state: True if filling, False if not filling.
function.clear()# Clears the window. The state and the position to the original default value.
turtle.screensize(w,h)# Sets the width and height of the canvas.
turtle.hideturtle()# Makes the turtle invisible.
turtle.showturtle()# Makes the turtle visible.
turtle.isbisible()# Returns the turtle visible.
turtle.write(s,font=("Arial",8,"normal"))# Writes the string s on the turtle position, Font is a triple consisting of fontname, fontsize, and fonttype.
Demo:
import turtle
turtle.pensize(3)
turtle.penup()
<(-100, -50)
turtle.pendown()
turtle.begin_fill()
turtle.circle(40, steps=3)
turtle.penup()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。