简体   繁体   中英

Python turtle.pensize() not changing the size of the pen

I am trying to change the pen size in Python 2.7. I know the pensize() command can be interchanged with width() (neither work), however the size of the pen stays the same no matter what value I enter. We use WingIDE to program in Python, though using the Python IDE doesn't fix the issue.

I have tested this on 3 machines, same issue. I have played with the positioning of the turtle.pensize() , size remains the same. The code below will draw the letter "I" and begin on a second letter:

import turtle

t = turtle.Pen()
turtle.bgcolor("black")
turtle.pensize(800)

t.reset()
t.color("green");

t.pu();
t.setx(-450);
t.pd();

t.left(90);

t.forward(120);

t.pu();
t.right(90);
t.forward(90);
t.right(90);
t.pd();

#I

t.forward(120);
t.left(90);
t.forward(20);

reset()
   

I don't really receive an error, but I will post what the WingIDE debugger displays:

> 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate JAMES ILLUMINATI TURTLE FINISHED p1.py]
Traceback (most recent call last):
  File "C:/Users/Com Lab 16/Dropbox/Student's Curriculum Folder/0. Student Files/James Dundon/JAMES ILLUMINATI TURTLE FINISHED p1.py", line 50, in <module>
    t.right(90);
  File "c:\Python27\Lib\lib-tk\turtle.py", line 1594, in right
    self._rotate(-angle)
  File "c:\Python27\Lib\lib-tk\turtle.py", line 3109, in _rotate
    self._update()
  File "c:\Python27\Lib\lib-tk\turtle.py", line 2565, in _update
    self._update_data()
  File "c:\Python27\Lib\lib-tk\turtle.py", line 2551, in _update_data
    self.screen._incrementudc()
  File "c:\Python27\Lib\lib-tk\turtle.py", line 1240, in _incrementudc
    raise Terminator
turtle.Terminator:

First time posting here, please let me know if I can be more specific or add additional information.

however the size of the pen stays the same no matter what value i enter

And well it should as you are in fact working with two different turtles :

t = turtle.Pen()
turtle.bgcolor("black")
turtle.pensize(800)
...
t.forward(120)

You are making the mistake of mixing the functional interface to turtle with the object-oriented interface to turtle. You created your own turtle t , but you changed the pen size of the default turtle. If you want to change your turtle's pen size, you do:

t.pensize(800)

This is a common error so I recommend that Python turtle programmers use this import:

from turtle import Screen, Turtle

It loads the object-oriented code but blocks out the functional interface. Once that's fixed, then you need to address the issue raised by @jasonharper in the comments that calling reset() undoes pensize() and really isn't needed at this point in the program. Your code rewritten accordingly:

from turtle import Screen, Turtle, mainloop

screen = Screen()
screen.bgcolor('black')

t = Turtle()
t.pensize(8)

t.color("green")

t.penup()
t.setx(-450)
t.pendown()

t.left(90)
t.forward(120)

t.penup()
t.right(90)
t.forward(90)
t.right(90)
t.pendown()

t.forward(120)
t.left(90)
t.forward(20)

mainloop()  # screen.mainloop() for Python 3

You can try:

turtle.pensize.width() 

It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM