海龟画图

摘抄来源:https://blog.csdn.net/sandalphon4869/article/details/99443949

Turtle motion 海龟动作
Move and draw 移动和绘画
forward() | fd() 前进
backward() | bk() | back() 后退
right() | rt() 右转
left() | lt() 左转
goto() | setpos() | setposition() 前往/定位
setx() 设置x坐标
sety() 设置y坐标
setheading() | seth() 设置朝向
home() 返回原点
circle() 画圆
dot() 画点
stamp() 印章
clearstamp() 清除印章
clearstamps() 清除多个印章
undo() 撤销
speed() 速度
Tell Turtle’s state 获取海龟的状态
position() | pos() 位置
towards() 目标方向
xcor() x坐标
ycor() y坐标
heading() 朝向
distance() 距离
Setting and measurement 设置与度量单位
degrees() 角度
radians() 弧度
Pen control 环比控制
Drawing state 绘图状态
pendown() | pd() | down() 画笔落下
penup() | pu() | up() 画笔抬起
pensize() | width() 画笔粗细
pen() 画笔
isdown() 画笔是否落下
Color control 颜色控制
color() 颜色
pencolor() 画笔颜色
fillcolor() 填充颜色
Filling 填充
filling() 是否填充
begin_fill() 开始填充
end_fill() 结束填充
More drawing control 更多绘画控制
reset() 重置
clear() 清空
write() 书写
Turtle state 海龟状态
Visibility 可见性
showturtle() | st() 显示海龟
hideturtle() | ht() 隐藏海龟
isvisible() 是否可见
Appearance 外观
shape() 形状
resizemode() 大小调整模式
shapesize() | turtlesize() 形状大小
shearfactor() 剪切因子
settiltangle() 设置倾角
tiltangle() 倾角
tilt() 倾斜
shapetransform() 变形
get_shapepoly() 获取形状多边形
Using events 使用事件
onclick() 当鼠标点击
onrelease() 当鼠标释放
ondrag() 当鼠标拖动
Special Turtle methods 特殊海龟方法
begin_poly() 开始记录多边形
end_poly() 结束多边形
get_poly() 获取多边形
clone() 克隆
getturtle() | getpen() 获取海龟画笔
getscreen() 获取屏幕
setundobuffer() 设置缓冲区条目数
undobufferentries() 撤销缓冲区条目数
Methods of TurtleScreen/Screen
Window control 窗口控制
bgcolor() 背景颜色
bgpic() 背景图片
clear() | clearscreen() 清屏
reset() | resetscreen() 重置
screensize() 屏幕大小
setworldcoordinates() 设置世界坐标系
Animation control 动画控制
delay() 延迟
tracer() 追踪
update() 更新
Using screen events 使用屏幕事件
listen() 监听
onkey() | onkeyrelease() 当键盘按下并释放
onkeypress() 当键盘按下
onclick() | onscreenclick() 当点下屏幕
ontimer() 当达到定时
mainloop() | done() 主循环
Settings and special methods 设置与特殊方法
mode() 模式
colormode() 颜色模式
getcanvas() 获取画布
getshapes() 获取形状
register_shape() | addshape() 添加形状
turtles() 所有海龟
window_height() 窗口高度
window_width() 窗口宽度
Input methods 输入方法
textinput() 文本输入
numinput() 数字输入
Methods specific to Screen screen专有方法
bye() 退出
exitonclick() 当点击时退出
setup() 设置
title() 标题

四、海龟方法详解
1.海龟动作
(1)移动和绘制
turtle.forward(distance)
turtle.fd(distance)
1
2
参数:
distance – 一个数值 (整型或浮点型)

作用:
海龟前进 distance 指定的距离,方向为海龟的朝向

turtle.back(distance)
turtle.bk(distance)
turtle.backward(distance)
1
2
3
参数:
distance – 一个数值

作用:
海龟后退 distance 指定的距离,方向与海龟的朝向相反。不改变海龟的朝向

turtle.right(angle)
turtle.rt(angle)
1
2
参数:
angle – 一个数值 (整型或浮点型)

作用:
海龟右转 angle 个单位。(单位默认为角度,但可通过 degrees() 和 radians() 函数改变设置。)

turtle.left(angle)
turtle.lt(angle)
1
2
参数:
angle – 一个数值 (整型或浮点型)

作用:
海龟左转 angle 个单位。(单位默认为角度,但可通过 degrees() 和 radians() 函数改变设置。)

turtle.goto(x, y=None)
turtle.setpos(x, y=None)
turtle.setposition(x, y=None)
1
2
3
参数:

x – 一个数值或数值对/向量
y – 一个数值或 None
如果 y 为 None,x 应为一个表示坐标的数值对或 Vec2D 类对象 (例如 pos() 返回的对象).

作用:
海龟移动到一个绝对坐标。如果画笔已落下将会画线。不改变海龟的朝向。

例子:

# x和y
turtle.setpos(60,30)
# x为元组
turtle.setpos((20,80))
# x为pos()返回的元组
tp = turtle.pos()
turtle.setpos(tp)
1
2
3
4
5
6
7
turtle.setx(x)
1
参数:
x – 一个数值 (整型或浮点型)

作用:
设置海龟的横坐标为 x,纵坐标保持不变。

turtle.sety(y)
1
参数:
y – 一个数值 (整型或浮点型)

作用:
设置海龟的纵坐标为 y,横坐标保持不变。

turtle.setheading(to_angle)
turtle.seth(to_angle)
1
2
参数:
to_angle – 一个数值 (整型或浮点型)

作用:
设置海龟的朝向为 to_angle。

turtle.home()
1
作用:
海龟移至初始坐标 (0,0),并设置朝向为初始方向 (由海龟模式确定,参见 mode())。

turtle.circle(radius, extent=None, steps=None)
1
参数:

radius – 一个数值 (整型或浮点型),表示半径
如果 radius 为正值则圆心在乌龟头的左边,否则圆心在乌龟头的右边。

extent – 一个数值 (整型或浮点型),表示角度
如未指定 extent则绘制整个圆,指定角度(可以大于360°)则绘制圆弧。
如果 radius 为正值则按乌龟的前进的方向,否则按乌龟的倒退的方向。

steps – 一个整型数
如未指定 steps则绘制圆弧,指定则绘制多边形(边数大于0)。

作用:
绘制一个 radius 指定半径的多边形部分。

例子:

# 绘制在左边的、按前进方向的
import turtle
turtle.circle(50,270)
turtle.done()
1
2
3
4


# 绘制在右边的、按前进方向的
import turtle
turtle.circle(50,270)
turtle.done()
1
2
3
4


# 绘制在左边的、按后退方向的
import turtle
turtle.circle(50,270)
turtle.done()
1
2
3
4


# 绘制在右边的、按后退方向的
import turtle
turtle.circle(50,270)
turtle.done()
1
2
3
4


# 绘制三角形
import turtle
turtle.circle(50,360,3)
turtle.done()
1
2
3
4


turtle.dot(size=None, *color)
1
参数:

size – 一个整型数 >= 1 (如果指定)
如果 size 未指定,则直径取 pensize+4 和 2*pensize 中的最大值。
color – 一个颜色字符串或颜色数值元组
未指定则是画笔颜色。
作用:
绘制一个直径为 size,颜色为 color 的圆点。

例子:
turtle.dot(),turtle.dot(50),turtle.dot("blue"),turtle.dot(50,"blue")

import turtle
turtle.dot(50,"blue")
turtle.done()
1
2
3


turtle.undo()
1
作用:
撤消 (或连续撤消) 最近的一个 (或多个) 海龟动作。可撤消的次数由撤消缓冲区的大小决定。

turtle.speed(speed=None)
1
参数:

speed – 一个 0到10 范围内的整型数或速度字符串
无参:返回当前海龟的速度
speed对应表:
如果输入数值大于 10 或小于 0.5 则速度设为 0。

字符串 数字 含义
“fastest” 0 最快
“fast” 10 快
“normal” 6 正常
“slow” 3 慢
“slowest” 1 最慢
速度值从 1 到 10,画线和海龟转向的动画效果逐级加快。speed = 0 表示没有动画效果(转向),但绘制还是需要时间,最快。

作用:
设置海龟移动的速度或返回当前海龟的速度。

(2)获取海龟状态
turtle.position()
turtle.pos()
1
2
作用:
返回海龟当前的坐标 (x,y) (为 Vec2D 矢量类对象)。

turtle.towards(x, y=None)
1
参数:

x – 一个数值或数值对/矢量,或一个海龟实例
y – 一个数值——如果 x 是一个数值,否则为 None
返回一个角度,从海龟位置到到 (x,y)的矢量到海龟初始朝向的矢量的夹角。

例子:

import turtle
turtle.goto(50,50)
print(turtle.towards(0,0))
turtle.done()
# 225.0
# 海龟朝向0°,从(50,50)到(0,0)的矢量朝向225°,两者相差225.0
1
2
3
4
5
6


turtle.xcor()
1
作用:
返回海龟的 x 坐标。

turtle.ycor()
1
作用:
返回海龟的 y 坐标。

turtle.heading()
1
作用:
返回海龟当前的朝向的角度

turtle.distance(x, y=None)
1
参数:

x – 一个数值或数值对/矢量,或一个海龟实例
y – 一个数值——如果 x 是一个数值,否则为 None
返回从海龟位置到 (x,y)的单位距离。

(3)度量单位设置
turtle.degrees(fullcircle=360.0)
1
参数:
fullcircle – 一个数值

作用:
设置一个圆周为多少 “度”。默认值为 360 度。

>>> turtle.home()
>>> turtle.left(90)
>>> turtle.heading()
90.0

>>> turtle.degrees(400.0)
>>> turtle.heading()
100.0
# 原来的朝向占一个圆的四分之一,90/360。现在,还要占四分之一,那就是400/4=100°
1
2
3
4
5
6
7
8
9
turtle.radians()
1
设置角度的度量单位为弧度。其值等于 度÷180×π度\div 180 \times \pi度÷180×π。

>>> turtle.home()
>>> turtle.left(90)
>>> turtle.heading()
90.0
>>> turtle.radians()
>>> turtle.heading()
1.5707963267948966
# 90/180*3.14=1.57...
1
2
3
4
5
6
7
8
2.画笔控制
(1)绘图状态
turtle.pendown()
turtle.pd()
turtle.down()
1
2
3
作用:
画笔落下 – 移动时将画线。

turtle.penup()
turtle.pu()
turtle.up()
1
2
3
作用:
画笔抬起 – 移动时不画线。

turtle.pensize(width=None)
turtle.width(width=None)
1
2
参数:

width – 一个正数值
无参:返回画笔的线条粗细。
作用:
设置线条的粗细为 width 或返回该值。

turtle.isdown()
1
作用:
如果画笔落下返回 True,如果画笔抬起返回 False。

(2)颜色控制
turtle.pencolor(*args)
1
参数:

pencolor()
返回表示当前画笔颜色的颜色描述字符串或元组。

pencolor(colorstring)
设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串。
如:turtle.pencolor("brown")或turtle.pencolor('#32c18f')

pencolor((r, g, b))
设置画笔颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围应为0…colormode。
如:turtle.pencolor((0.2, 0.8, 0.55))或turtle.pencolor((51.0, 204.0, 140.0))

pencolor(r, g, b)
设置画笔颜色为以 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0…colormode。
如:turtle.pencolor(0.2, 0.8, 0.55)或turtle.pencolor(51.0, 204.0, 140.0)

作用:
返回或设置画笔颜色。

turtle.fillcolor(*args)
1
参数:同turtle.pencolor(*args)

fillcolor()
fillcolor(colorstring)
fillcolor((r, g, b))
fillcolor(r, g, b)
作用:
返回或设置填充颜色。

turtle.color(*args)
1
参数:

color()
返回以一对颜色描述字符串或元组表示的画笔颜色和填充颜色,两者可分别由 pencolor() 和 fillcolor() 返回。

color(colorstring), color((r,g,b)), color(r,g,b)
同时设置填充颜色和画笔颜色为指定的值。

color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))
相当于pencolor(colorstring1)加 fillcolor(colorstring2)

作用:
返回或设置画笔颜色和填充颜色。

(3)填充
turtle.filling()
1
作用:
返回填充状态 (填充为 True,否则为 False)。

turtle.begin_fill()
1
作用:
在绘制要填充的形状之前调用。要配合turtle.end_fill()使用。

turtle.end_fill()
1
作用:
填充上次调用 begin_fill() 之后绘制的形状。要配合turtle.begin_fill()使用。

例子:

import turtle
turtle.color("black", "red")
turtle.begin_fill()
turtle.circle(80)
turtle.end_fill()
turtle.done()
1
2
3
4
5
6


(4)更多绘图控制
turtle.reset()
1
作用:
从屏幕中删除海龟的绘图,海龟回到原点并设置所有变量为默认值。

turtle.clear()
1
作用:
从屏幕中删除指定海龟的绘图。不移动海龟。海龟的状态和位置以及其他海龟的绘图不受影响。

turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1
参数:

arg – 要书写到 TurtleScreen 的对象
如:"something",123,(1,2),[1,2,3]等

move – True/False
如果 move 为 True,画笔会移动到文本的右下角,这样文字就不会叠在一块了。

align – 字符串 “left”, “center” 或 “right”
align 指定对齐方式 (“left”, “center” 或 right")

font – 一个三元组 (fontname, fontsize, fonttype)
font 指定字体

书写文本 - arg 指定的字符串 - 到当前海龟位置。

例子:

import turtle
turtle.write("Home = ",True, align="center")
turtle.write( [123,456], True)
turtle.done()
1
2
3
4


3.海龟状态
(1)可见性
turtle.hideturtle()
turtle.ht()
1
2
作用:
使海龟不可见。当你绘制复杂图形时这是个好主意,因为隐藏海龟可显著加快绘制速度。

turtle.showturtle()
turtle.st()
1
2
作用:
使海龟可见。

turtle.isvisible()
1
作用:
如果海龟显示返回 True,如果海龟隐藏返回 False。

(2)外观
turtle.shape(name=None)
1
参数:

name – 一个有效的形状名字符串
无参:返回当前的形状名
设置海龟形状或返回当前的形状名。

形状名 图形
“arrow”
“blank”(透明)
“turtle”
“circle”
“square”
“triangle”
“classic”
turtle.register_shape(name, shape=None)
turtle.addshape(name, shape=None)
1
2
调用此函数有三种不同方式:

name 为一个 gif 文件的文件名, shape 为 None: 安装相应的图像形状。:
注解:当海龟转向时图像形状 不会 转动,因此无法显示海龟的朝向!

name 为指定的字符串,shape 为由坐标值对构成的元组: 安装相应的多边形形状。

import turtle
turtle.register_shape("myshape", ((-20,20), (0,-10), (50,0)))
turtle.shape("myshape")
turtle.done()
1
2
3
4
name 为指定的字符串, 为一个 (复合) Shape 类对象: 安装相应的复合形状。
作用:
将一个海龟形状加入 TurtleScreen 的形状列表。只有这样注册过的形状才能通过执行 shape(shapename) 命令来使用。

4.使用事件
turtle.onclick(fun, btn=1, add=None)
1
参数:

fun – 一个函数,调用时将传入两个参数表示在画布上点击的坐标。
如果 fun 值为 None,则移除现有的绑定。

btn – 鼠标按钮编号,默认值为 1 (鼠标左键)
左中右:1,2,3

add – True 或 False – 如为 True 则将添加一个新绑定,否则将取代先前的绑定

作用:
将 fun 指定的函数绑定到鼠标点击此海龟引发事件或移除绑定。(注意,不是随便点,是点击海龟)

例子:

import turtle

def turtlefd(x,y):
turtle.fd(100)

turtle.onclick(turtlefd)
turtle.done()


turtle.onrelease(fun, btn=1, add=None)
1
参数:

fun – 一个函数,调用时将传入两个参数表示在画布上点击的坐标。
如果 fun 值为 None,则移除现有的绑定。

btn – 鼠标按钮编号,默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定,否则将取代先前的绑定

作用:
将 fun 指定的函数绑定到在此海龟上释放鼠标按键事件。(注意,在海龟上点击后释放鼠标才有效,在别的地方点击把鼠标拖过来释放无效)

turtle.ondrag(fun, btn=1, add=None)
1
参数:

fun – 一个函数,调用时将传入两个参数表示在画布上点击的坐标。
如果 fun 值为 None,则移除现有的绑定。

btn – 鼠标按钮编号,默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定,否则将取代先前的绑定

作用:
将 fun 指定的函数绑定到在此海龟上移动鼠标事件。

例子:在此之后点击并拖动海龟可在屏幕上手绘线条 (如果画笔为落下)

import turtle

def turtlefd(x,y):
turtle.goto(x,y)

turtle.ondrag(turtlefd)
turtle.done()
1
2
3
4
5
6
7


5.特殊海龟方法
turtle.begin_poly()
1
开始记录多边形的顶点。当前海龟位置为多边形的第一个顶点。

turtle.end_poly()
1
停止记录多边形的顶点。当前海龟位置为多边形的最后一个顶点。它将连线到第一个顶点。

turtle.get_poly()
1
返回最新记录的多边形。

五、TurtleScreen/Screen 方法详解
1.窗口控制
turtle.bgcolor(*args)
1
参数:
同turtle.pencolor(*args)

作用:
设置或返回 TurtleScreen 的背景颜色。

turtle.bgpic(picname=None)
1
参数:

picname – 一个字符串, gif-文件名, “nopic”, 或 None
如果 picname 为一个文件名,则将相应图片设为背景。
如果 picname 为 “nopic”,则删除当前背景图片。
如果 picname 为 None,则返回当前背景图片文件名。
作用:
设置背景图片或删除背景或返回当前背景图片名称。

PS:路径还是“\”或“/”,https://blog.csdn.net/sandalphon4869/article/details/86767978

turtle.reset()
turtle.resetscreen()
1
2
作用:
删除所有海龟的全部绘图,重置屏幕上的所有海龟为其初始状态。

turtle.clear()
turtle.clearscreen()
1
2
作用:
不仅达到turtle.reset()的作用,而且删除背景片成白色背景,无事件绑定并启用追踪。

turtle.screensize(canvwidth=None, canvheight=None, bg=None)
1
参数:

canvwidth – 正整型数,以像素表示画布的新宽度值
这个值可以超过电脑屏幕的尺寸,可以展示一副超大的画布。

canvheight – 正整型数,以像素表示画面的新高度值
这个值可以超过电脑屏幕的尺寸,可以展示一副超大的画布。

bg – 颜色字符串或颜色元组,新的背景颜色

作用:

如未指定任何参数,则返回当前的 (canvaswidth, canvasheight)。
否则改变作为海龟绘图场所的画布大小。(通过此方法可以令之前绘制于画布之外的图形变为可见。可以使用滚动条观察画布的隐藏区域.)
PS:
画布大小还是以画布中心点为中心的,而且这个滑动条可以滑动展示的区域是展示图形的那个画布大小,而不是能一直无限地拖动。

2.动画控制
turtle.delay(delay=None)
1
参数:

delay – 正整型数
无参返回以毫秒数表示的延迟值 delay
作用:
设置或返回以毫秒数表示的延迟值 delay。(这约等于连续两次画布刷新的间隔时间。) 绘图延迟越长,动画速度越慢。

turtle.tracer(n=None, delay=None)
1
参数:

n – 非负整型数
如果指定 n 值,则只有每第 n 次屏幕刷新会实际执行。(可被用来加速复杂图形的绘制。)
如果调用时不带参数,则返回当前保存的 n 值。

delay – 非负整型数
第二个参数设置延迟值 (参见 delay())。

作用:
启用/禁用海龟动画并设置刷新图形的延迟时间。

turtle.update()
1
作用:
执行一次 TurtleScreen 刷新。在禁用追踪时使用。

3.使用屏幕事件
turtle.listen(xdummy=None, ydummy=None)
1
作用:
设置焦点到 TurtleScreen (以便接收按键事件)。使用两个 Dummy 参数以便能够传递 listen() 给 onclick 方法。

turtle.onkeypress(fun, key=None)
1
参数:

fun – 一个无参数的函数或 None
如果 fun 值为 None,则移除事件绑定。

key – 一个字符串: 键 (例如 “a”) 或键标 (例如 “space”)
如未指定键则绑定到任意键的按下事件。

作用:
绑定 fun 指定的函数到指定键的按下事件。注: 为了能够注册按键事件,必须得到焦点。(参见 listen() 方法。)

例子:

#按方向上键画正方形(可以一直按进行快速移动)
import turtle

def f():
turtle.fd(100)
turtle.left(90)

turtle.speed(0)
turtle.listen()
turtle.onkeypress(f,"Up")
#解除绑定 turtle.onkeypress(None,"Up")
turtle.done()
1
2
3
4
5
6
7
8
9
10
11
12
turtle.onkey(fun, key)
turtle.onkeyrelease(fun, key)
1
2
参数:

fun – 一个无参数的函数或 None
如果 fun 值为 None,则移除事件绑定。

key – 一个字符串: 键 (例如 “a”) 或键标 (例如 “space”)

作用:
绑定 fun 指定的函数到按键按下并释放事件。
注: 为了能够注册按键事件,TurtleScreen 必须得到焦点。(参见listen() 方法)

例子:

#按方向上键释放后开始画正方形
import turtle

def f():
turtle.fd(100)
turtle.left(90)

turtle.speed(0)
turtle.listen()
turtle.onkey(f,"Up")
#解除绑定 turtle.onkey(None,"Up")
turtle.done()
1
2
3
4
5
6
7
8
9
10
11
12
turtle.onscreenclick(fun, btn=1, add=None)
1
参数:

fun – 一个函数,调用时将传入两个参数表示在画布上点击的坐标。
如果 fun 值为 None,则移除现有的绑定。

btn – 鼠标按钮编号,默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定,否则将取代先前的绑定

作用:
绑定 fun 指定的函数到鼠标点击屏幕事件。

PS:turtle.onclick()和turtle.onscreenclick()不一样,前者是只有点击到海龟身上才行,后者是点击画布任意区域就行。

例子:

#点击画布任意区域,海龟移动到那里
import turtle

def f(x,y):
turtle.goto(x,y)

turtle.onscreenclick(f)
turtle.done()
1
2
3
4
5
6
7
8
turtle.ontimer(fun, t=0)
1
参数:

fun – 一个无参数的函数

t – 一个数值 >= 0

作用:
安装一个计时器,在 t 毫秒后调用 fun 函数。

例子:

import turtle

def f():
turtle.fd(100)

turtle.ontimer(f,100)
turtle.done()
1
2
3
4
5
6
7
turtle.mainloop()
turtle.done()
1
2
必须作为一个海龟绘图程序的结束语句。要不然程序会无响应。

4.设置与特殊方法
turtle.mode(mode=None)
1
参数:

mode – 字符串 “standard”, “logo” 或 “world” 其中之一
无参:如未指定模式则返回当前的模式。
作用:
设置海龟模式 (“standard”, “logo” 或 “world”) 并执行重置。

模式 初始海龟朝向 正数角度
“standard” 朝右 (东) 逆时针
“logo” 朝上 (北) 顺时针
turtle.colormode(cmode=None)
1
参数:

cmode – 数值 1.0 或 255 其中之一
无参:返回colormode
作用:
返回颜色模式或将其设为 1.0 或 255。构成颜色三元组的 r, g, b 数值必须在 0…cmode 范围之内,否则报错

例子:

import turtle

turtle.pensize(10)

turtle.colormode(1.0)
turtle.pencolor(0.1,0.2,0.3)
turtle.fd(100)

turtle.colormode(255)
turtle.pencolor(200,20,133)
turtle.fd(100)

turtle.done()

turtle.window_height()
1
返回海龟窗口的高度。:

turtle.window_width()
1
返回海龟窗口的宽度。

5.输入方法
turtle.textinput(title, prompt)
1
参数:

title – 字符串
形参 title 为对话框窗口的标题

prompt – 字符串
prompt 为一条文本,通常用来提示要输入什么信息

作用:
弹出一个对话框窗口用来输入一个字符串,并返回输入的字符串。如果对话框被取消则返回 None。:

例子

import turtle
str=turtle.textinput("Hello","what's your ID?")
print(str)
turtle.done()

turtle.numinput(title, prompt, default=None, minval=None, maxval=None)
1
参数:

title – 字符串
title 为对话框窗口的标题

prompt – 字符串
prompt 为一条文本,通常用来描述要输入的数值信息。

default – 数值 (可选)
默认值

minval – 数值 (可选)
可输入的最小值

maxval – 数值 (可选)
可输入的最大值。
输入数值的必须在指定的 minval … maxval 范围之内,否则将给出一条提示,对话框保持打开等待修改。

作用:
弹出一个对话框窗口用来输入一个数值。返回输入的数值。如果对话框被取消则返回 None。

例子:

import turtle
num=turtle.numinput("Hello","what's your ID?",34,1,100)
print(num)
turtle.done()


6.Screen 专有方法
turtle.bye()
1
作用:
关闭海龟绘图窗口。

turtle.exitonclick()
1
作用:
将 bye() 方法绑定到 Screen 上,鼠标点击屏幕退出。

turtle.setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1
作用:
设置主窗口的大小和位置。默认参数值保存在配置字典中,可通过 turtle.cfg 文件进行修改。

参数:

width – 如为一个整型数值,表示大小为多少像素,如为一个浮点数值,则表示屏幕的占比;默认为屏幕的 50%

height – 如为一个整型数值,表示高度为多少像素,如为一个浮点数值,则表示屏幕的占比;默认为屏幕的 75%

startx – 如为正值,表示初始位置距离屏幕左边缘多少像素,负值表示距离右边缘,None 表示窗口水平居中

starty – 如为正值,表示初始位置距离屏幕上边缘多少像素,负值表示距离下边缘,None 表示窗口垂直居中

八、总结
1.最快的画笔速度
快:画笔速度为最快,但其实还是保留了绘制的过程
turtle.speed(0)
1
更快:不仅画笔速度快,更省略了绘画过程的延迟
turtle.speed(0)
turtle.delay(0)
1
2
超快:不展现绘制过程,所以超快
turtle.tracer(0)
1
比较的例子:

# 快
import turtle
turtle.speed(0)
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
turtle.color(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
turtle.done()

# 更快
import turtle
turtle.speed(0)
turtle.delay(0)
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
turtle.color(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
turtle.done()

# 超快
import turtle
turtle.tracer(0)
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
turtle.color(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
turtle.done()

2.类的使用
import turtle

def bye(x,y):
turtle.bye()

s = turtle.Screen()
s.bgcolor("black")
s.screensize(800,800)
s.title("Class Using")
s.onscreenclick(bye)

p=turtle.Turtle()
p.speed(0)
p.hideturtle()
p.pencolor("red")
p.pensize(3)
p.circle(50,360,6)

turtle.done()

猜你喜欢

转载自www.cnblogs.com/primula/p/12431268.html