Is it possible to change turtle's pen stroke?

To answer the question asked in the title: No, it is not possible to change the pen stroke directly (see cdlane's answer for a possible way to do it by modifying the hardcoded values from tkinter).

I did find a workaround for the use case presented in the question body, however.

A custom pen shape (in this case, representing the exact shape and size of the bar) can be registered like this:

screen.register_shape("bar", ((width / 2, 0), (-width / 2, 0), (-width / 2, height), (width / 2, height)))`

We can then simply loop through each bar, update the pen shape with the new values, and use turtle.stamp to stamp the completed bars onto the graph, no drawing required.


It looks like changing the shape of the pen stroke itself isn't possible. turtle.shape('square') only changes the shape of the turtle, not the pen stroke. I suggest lowering the pen size, and creating a function to draw a rectangle. You could use this do draw the bars.


I've two solutions to this problem that I've used in various programs.

The first is a variation on your stamp solution. Rather than use screen.register_shape() to register a custom polygon for each line, use a square turtle and for each line turtle.turtlesize() it into the rectangle you want to stamp:

from turtle import Turtle, Screen

STAMP_SIZE = 20  # size of the square turtle shape

WIDTH, LENGTH = 25, 125

yertle = Turtle(shape="square")
yertle.penup()

yertle.turtlesize(WIDTH / STAMP_SIZE, LENGTH / STAMP_SIZE)

yertle.goto(100 + LENGTH//2, 100)  # stamps are centered, so adjust X

yertle.stamp()

screen = Screen()
screen.exitonclick()

My other solution, when I need to draw instead of stamp, is to reach into turtle's tkinter underpinning and modify turtle's hardcoded line end shape itself:

from turtle import Turtle, Screen
import tkinter as _

_.ROUND = _.BUTT

WIDTH, LENGTH = 25, 125

yertle = Turtle()
yertle.width(WIDTH)
yertle.penup()

yertle.goto(100, 100)

yertle.pendown()

yertle.forward(LENGTH)

screen = Screen()
screen.exitonclick()