turtle.begin_fill() function in Python

Call the turtle.begin_fill() function before drawing a shape to be filled. The turtle will remember the start point of the shape.

Syntax: turtle.begin_fill()
Parameters: None.

Let us see an example to implement the turtle.begin_fill() function in Python:

Demo28.py

# turtle.begin_fill() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

t.fillcolor("yellow")
t.begin_fill()          # Start recording vertices for filling
for _ in range(4):
    t.forward(100)
    t.left(90)
t.end_fill()            # Fill the shape

window.exitonclick()

The following is the output:

turtle.begin_fill() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle (your pen).
  4. Set fill color: t.fillcolor(“yellow”) sets the interior color used when filling shapes to yellow.
  5. Begin filling: t.begin_fill() starts recording the path you draw next so it can be filled.
  6. Draw the square:
    • Loop: for _ in range(4): repeats the next two commands four times.
    • Side: t.forward(100) draws one side of length 100.
    • Turn: t.left(90) turns 90° to make right angles.
  7. End filling: t.end_fill() fills the traced square with yellow (the outline remains whatever the current pen color is).
  8. Close on click: window.exitonclick() keeps the window open until you click, then closes.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.filling() function in Python
turtle.end_fill() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment