Skip to content

Make turtle.pensize smarter #133244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
adorilson opened this issue May 1, 2025 · 3 comments · May be fixed by #135268
Open

Make turtle.pensize smarter #133244

adorilson opened this issue May 1, 2025 · 3 comments · May be fixed by #135268
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@adorilson
Copy link
Contributor

adorilson commented May 1, 2025

Feature or enhancement

Proposal:

The turtle.pensize doc says:

turtle.pensize(width=None)
    Parameters:

        width – a positive number

However, if you put a non-positive number, the function will accept it without any error or warning.

Afterwards, a draw action (like forward) will raise an exception. See below.

>>> import turtle
>>> turtle.pensize(-1)
>>> turtle.forward(100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 8, in forward
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 1636, in forward
    self._go(distance)
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 1597, in _go
    self._goto(ende)
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 3248, in _goto
    screen._drawline(self.drawingLineItem,
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 543, in _drawline
    self.cv.itemconfigure(lineitem, width=width)
  File "<string>", line 1, in itemconfigure
  File "/home/adorilson/anaconda3/lib/python3.12/tkinter/__init__.py", line 2988, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adorilson/anaconda3/lib/python3.12/tkinter/__init__.py", line 1712, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: bad screen distance "-1"
>>>

My suggestion is pensize (and probably others) raises a TurtleGraphicsError if the width argument is less than 1.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

https://discuss.python.org/t/improving-the-turtle-library/61840/18

Linked PRs

@adorilson adorilson added the type-feature A feature request or enhancement label May 1, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label May 1, 2025
@StanFromIreland
Copy link
Contributor

if the width argument is less than 1.

This is a backwards compatibility concern, since currently values less than one are accepted, even though they are equivalent to 1. This should be a separate feature issue.

@donBarbos
Copy link
Contributor

donBarbos commented May 1, 2025

This is a backwards compatibility concern, since currently values less than one are accepted, even though they are equivalent to 1. This should be a separate feature issue.

the thing is that anyone who uses negative values ​​still gets an error, just in a different place. it's unlikely that anyone calls pensize() without further action.

maybe wait to see what @willingc as a turtle expert says

I would also like to note that other methods expect positive numbers arguments: pensize (width alias), pen, shapesize (turtlesize alias), tracer (expecting nonnegative integer)

and shapesize has argument checking but only for zero:

cpython/Lib/turtle.py

Lines 2913 to 2914 in ad2f088

if stretch_wid == 0 or stretch_len == 0:
raise TurtleGraphicsError("stretch_wid/stretch_len must not be zero")

adorilson added a commit to adorilson/cpython that referenced this issue Jun 8, 2025
adorilson added a commit to adorilson/cpython that referenced this issue Jun 8, 2025
@adorilson
Copy link
Contributor Author

This is a backwards compatibility concern, since currently values less than one are accepted, even though they are equivalent to 1. This should be a separate feature issue.

In fact, if it is 0, it works this way, but if it is a negative number, it doesn't. I took it into mind while opening the PR, and put in there an idea concerning 0.

adorilson added a commit to adorilson/cpython that referenced this issue Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
4 participants