Search code examples
pythonturtle-graphicspython-turtle

Changing only fill or line color in Python turtle


I want to change either the line or the fill color with a key, but not both. I've tried adding a dummy word in front of the comma, but it didn't work.

import turtle
from turtle import Turtle, Screen

screen = Screen()

PenWidth = int(input("Enter your Penwidth"))
jack = Turtle("turtle")
jack.color("red", "green")
jack.pensize(PenWidth)
jack.speed(0)

 
def blueLine():
    jack.color("blue")
def blueFill():
    jack.color("blue")
def up():
    jack.setheading(90)
    jack.forward(100)



turtle.listen()

turtle.onkey(up, "Up")
turtle.onkey(blueLine, "1")
turtle.onkey(blueFill,"+")

screen.mainloop()

Solution

  • I want to change either the line or the fill color with a key, but not both.

    The simple answer is to invoke pencolor() and fillcolor() rather than color():

    from turtle import Turtle, Screen
    
    def blueLine():
        jack.pencolor('blue')
    
    def blueFill():
        jack.fillcolor('blue')
    
    def up():
        jack.setheading(90)
        jack.forward(100)
    
    penWidth = int(input("Enter your pen width: "))
    
    screen = Screen()
    
    jack = Turtle('turtle')
    jack.color('red', 'green')
    jack.pensize(penWidth)
    jack.speed('fastest')
    
    screen.onkey(up, 'Up')
    screen.onkey(blueLine, '1')
    screen.onkey(blueFill, '+')
    screen.listen()
    
    screen.mainloop()