How to implement closed shape color filling in Python?
In Python, you can use the turtle library to draw closed shapes and fill them with color. Here is an example code demonstrating how to draw a closed shape and fill it with color:
import turtle
# 创建一个画布
canvas = turtle.Screen()
# 创建一个画笔
pen = turtle.Turtle()
# 绘制图形
pen.begin_fill() # 开始填充
pen.fillcolor("red") # 设置填充颜色
pen.pensize(3) # 设置画笔宽度
# 绘制封闭图形
pen.forward(100)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(100)
pen.end_fill() # 结束填充
# 关闭画布
canvas.exitonclick()
In the example above, we first imported the turtle library, then created a canvas and a pen. Next, we began filling using the begin_fill() method, set the fill color using fillcolor() method, and set the pen width using pensize() method. We then used the forward() and left() methods to draw a closed shape. Finally, we ended the filling with the end_fill() method. Lastly, we used the exitonclick() method to exit the program when clicking on the canvas.