使用turtle画个图,怎么把图内部填充上颜色
时间: 2023-03-22 13:01:43 浏览: 171
要在turtle画的图形内部填充颜色,可以使用turtle库中的fill函数和begin_fill函数以及end_fill函数。例如,下面的代码将绘制一个红色的正方形并填充它的内部:
```python
import turtle
t = turtle.Turtle()
t.begin_fill() # 开始填充
t.color("red")
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.end_fill() # 结束填充
turtle.done()
```
在begin_fill和end_fill之间的所有绘图操作将会填充指定的颜色。你可以替换上述代码中的红色颜色值以使用不同的颜色。
相关问题
Python调用turtle库画一个五边形并内部填充红色
好的,这里是使用Python调用turtle库画一个五边形并内部填充红色的代码:
```python
import turtle
# 创建一个Turtle实例
t = turtle.Turtle()
# 设置画笔颜色和画笔宽度
t.color("red")
t.pensize(3)
# 开始填充
t.begin_fill()
# 循环画五边形的五条边
for i in range(5):
t.forward(100) # 向前走100个单位长度
t.right(72) # 向右转72度
# 结束填充
t.end_fill()
# 隐藏画笔
t.hideturtle()
# 显示画布
turtle.done()
```
运行这段代码,就可以在画布上看到一个内部填充红色的五边形了。
turtle画五角星中间没有填充
在Python的Turtle模块中,如果你想画一个五角星但不想填充颜色,你可以先设置线条样式为“none”来关闭填充,然后再绘制五角星。下面是一个简单的例子:
```python
import turtle
# 创建turtle对象并隐藏它
t = turtle.Turtle()
t.speed(0) # 设置最快的速度
t.hideturtle()
# 设置线条样式为无填充
t.pencolor("black") # 设定颜色
t.fillcolor("none") # 关闭填充
t.begin_fill() # 开始填充(因为已设为无,这一步其实是多余的)
# 绘制五角星
for _ in range(5): # 画5条边
t.forward(100)
t.right(72)
# 结束填充(虽然前面已经设定,这里还是需要)
t.end_fill()
# 隐藏turtle并防止窗口关闭时退出程序
t.hideturtle()
turtle.done() # 等待用户关闭窗口
```
在这个示例中,五角星的每一笔都是黑色的,但是不会有任何填充色。如果你直接运行这个程序,屏幕上会出现一个没有填充颜色的五角星。
阅读全文
相关推荐















