python turtle隐藏画笔_Turtle库学习--画笔控制 绘图状态

本文介绍了Python Turtle库中的画笔控制方法,包括隐藏与显示画笔、设置线条粗细、颜色及填充颜色。通过示例代码展示了如何使用pendown()、penup()、pensize()、pen()等函数来控制绘图状态,并提供了填充形状的begin_fill()和end_fill()函数的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

turtle.pendown()

简:turtle.pd()

turtle.down()

画笔落下 – 移动时将画线。

turtle.penup()

简:turtle.pu()

turtle.up()

画笔抬起 – 移动时不画线。

turtle.pensize(width=None)

turtle.width(width=None)

参数

width – 一个正数值

设置线条的粗细为 width 或返回该值

turtle.pen(pen=None, **pendict)

参数

pen – 一个包含部分或全部下列键的字典

pendict – 一个或多个以下列键为关键字的关键字参数

返回或设置画笔的属性,以一个包含以下键值对的 “画笔字典” 表示:

“shown”: True/False

“pendown”: True/False

“pencolor”: 颜色字符串或颜色元组

“fillcolor”: 颜色字符串或颜色元组

“pensize”: 正数值

“speed”: 0…10 范围内的数值

“resizemode”: “auto” 或 “user” 或 “noresize”

“stretchfactor”: (正数值, 正数值)

“outline”: 正数值

“tilt”: 数值

turtle.isdown()

如果画笔落下返回 True,如果画笔抬起返回 False。

运行以下代码观察结果:

import turtle

turtle.penup()

print(turtle.isdown())

turtle.pendown()

print(turtle.isdown())

turtle.pen({

"shown": True,

"pendown": True,

"fillcolor":"red",

})

print(sorted(turtle.pen().items()))

turtle.done()

turtle.pencolor(*args)

返回或设置画笔颜色

允许以下四种输入格式:

pencolor()

返回以颜色描述字符串或元组 (见示例) 表示的当前画笔颜色。可用作其他 color/pencolor/fillcolor 调用的输入。

pencolor(colorstring)

设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串,例如 “red”、“yellow” 或 “#33cc8c”。

pencolor((r, g, b))

设置画笔颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围应为 0…colormode,colormode 的值为 1.0 或 255 (参见 colormode())。

pencolor(r, g, b)

设置画笔颜色为以 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0…colormode

turtle.fillcolor(*args)

返回或设置填充颜色

允许输入格式与turtle.pencolor一样

turtle.color(*args)

返回或设置画笔颜色和填充颜色

运行以下代码观察结果:

import turtle

turtle.pencolor("red")

turtle.fillcolor("yellow")

print(turtle.color())

turtle.done()

turtle.filling()

返回填充状态 (填充为 True,否则为 False)

turtle.begin_fill()

在绘制要填充的形状之前调用。

turtle.end_fill()

填充上次调用 begin_fill() 之后绘制的形状

运行以下代码观察结果:

import turtle

turtle.pencolor("red")

turtle.fillcolor("yellow")

print(turtle.color())

print(turtle.filling())

turtle.pen({

"fillcolor":"red",

"pencolor":"green"

})

turtle.begin_fill()

for i in range(4):

turtle.forward(100)

turtle.left(90)

turtle.end_fill()

turtle.done()

turtle.reset()

从屏幕中删除海龟的绘图,海龟回到原点并设置所有变量为默认值

turtle.clear()

从屏幕中删除指定海龟的绘图。不移动海龟

海龟的状态和位置以及其他海龟的绘图不受影响

运行以下代码观察结果:

import turtle

turtle.pencolor("red")

turtle.fillcolor("yellow")

print(turtle.color())

print(turtle.filling())

turtle.pen({

"fillcolor":"red",

"pencolor":"green"

})

turtle.begin_fill()

for i in range(4):

turtle.forward(100)

turtle.left(90)

turtle.end_fill()

turtle.reset()

for i in range(5):

turtle.forward(100)

turtle.left(60)

turtle.clear()

for i in range(3):

turtle.forward(100)

turtle.left(120)

for i in range(2):

turtle.undo()

turtle.done()

原文链接:https://blog.csdn.net/hide_in_darkness/article/details/107629792

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值