python填充颜色的代码
以下是Python中填充颜色的代码示例:
import turtle
# 创建一个画布
canvas = turtle.Screen()
# 创建一个乌龟对象
t = turtle.Turtle()
# 设置填充颜色为红色
t.fillcolor("red")
# 开始填充
t.begin_fill()
# 绘制一个正方形
for i in range(4):
t.forward(100)
t.right(90)
# 结束填充
t.end_fill()
# 隐藏乌龟
t.hideturtle()
# 点击关闭按钮退出程序
canvas.exitonclick()
运行以上代码,将会绘制一个红色的正方形,并填充颜色。其中,fillcolor()方法用于设置填充颜色,begin_fill()方法用于开始填充,end_fill()方法用于结束填充。
除了上面的示例代码,Python中还有其他填充颜色的方法,具体如下:
使用color()方法设置画笔颜色和填充颜色,然后使用begin_fill()和end_fill()方法填充颜色。示例代码如下:
import turtle
# 创建一个画布
canvas = turtle.Screen()
# 创建一个乌龟对象
t = turtle.Turtle()
# 设置画笔颜色和填充颜色为红色
t.color("red", "red")
# 开始填充
t.begin_fill()
# 绘制一个正方形
for i in range(4):
t.forward(100)
t.right(90)
# 结束填充
t.end_fill()
# 隐藏乌龟
t.hideturtle()
# 点击关闭按钮退出程序
canvas.exitonclick()
使用pencolor()方法设置画笔颜色,使用fillcolor()方法设置填充颜色,然后使用begin_fill()和end_fill()方法填充颜色。示例代码如下:
import turtle
# 创建一个画布
canvas = turtle.Screen()
# 创建一个乌龟对象
t = turtle.Turtle()
# 设置画笔颜色为黑色,填充颜色为红色
t.pencolor("black")
t.fillcolor("red")
# 开始填充
t.begin_fill()
# 绘制一个正方形
for i in range(4):
t.forward(100)
t.right(90)
# 结束填充
t.end_fill()
# 隐藏乌龟
t.hideturtle()
# 点击关闭按钮退出程序
canvas.exitonclick()
以上两种方法都可以实现填充颜色的效果,具体使用哪种方法取决于个人喜好和需求。