Is it possible to change turtle's pen stroke?

Matt picture Matt · Dec 2, 2014 · Viewed 8.8k times · Source

I need to draw a bar graph using Python's turtle graphics and I figured it would be easier to simply make the pen a thick square so I could draw the bars like that and not have to worry about making dozens of rectangles and filling them in.

When I set the turtle shape using turtle.shape('square') though, it only changes the appearance of the pen but has no effect on the actual drawing:

enter image description here

Is there a way to make turtle actually draw a rectangular stroke, whether that be through built-in methods or through modifying the turtle file?

I DON'T want rounded edges, like this:

enter image description here

Answer

Matt picture Matt · Dec 2, 2014

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.