19 Sep turtle.width() function in Python
The turtle.width() function sets the width of the pen line. A larger number draws a thicker line.
Syntax: turtle.width(width=None)
Parameters: width – a positive number representing the new line width in pixels.
Let us see an example to implement the turtle.width() function in Python:
Demo21.py
# turtle.width() function in Python # Code by Studyopedia import turtle window = turtle.Screen() t = turtle.Turtle() t.width(1) # Set pen width to 1 t.forward(50) t.width(5) # Set pen width to 5 t.forward(50) t.width(10) # Set pen width to 10 t.forward(50) window.exitonclick()
The following is the output:

In the above code, we followed the below steps:
- Import the module: import turtle loads Python’s turtle graphics library.
- Create the screen: window = turtle.Screen() opens the drawing window.
- Create the turtle: t = turtle.Turtle() creates the turtle (your pen).
- Set width to 1: t.width(1) sets the pen thickness to 1 pixel; it affects only future strokes.
- Draw with width 1: t.forward(50) draws a thin 50-unit line.
- Set width to 5: t.width(5) increases the pen thickness to 5 pixels for subsequent drawing.
- Draw with width 5: t.forward(50) draws a medium 50-unit line.
- Set width to 10: t.width(10) sets a thick 10-pixel pen width for next strokes.
- Draw with width 10: t.forward(50) draws a thick 50-unit line. Previously drawn lines keep their original widths.
- 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:
- OpenCV Tutorial
- Python Tutorial
- NumPy Tutorial
- Pandas Tutorial
- Matplotlib Tutorial
- Generative AI Tutorial
- LangChain Tutorial
- RAG Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- Gemini Tutorial
- ChatGPT Tutorial
No Comments