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:

turtle.width() 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 width to 1: t.width(1) sets the pen thickness to 1 pixel; it affects only future strokes.
  5. Draw with width 1: t.forward(50) draws a thin 50-unit line.
  6. Set width to 5: t.width(5) increases the pen thickness to 5 pixels for subsequent drawing.
  7. Draw with width 5: t.forward(50) draws a medium 50-unit line.
  8. Set width to 10: t.width(10) sets a thick 10-pixel pen width for next strokes.
  9. Draw with width 10: t.forward(50) draws a thick 50-unit line. Previously drawn lines keep their original widths.
  10. 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.isdown() function in Python
turtle.pen() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment