仅更改 Python 海龟中的填充或线条颜色
Changing only fill or line color in Python turtle
我想用键更改线条或填充颜色,但不能同时更改两者。我试过在逗号前加一个虚拟词,但没用。
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()
turtle.color()
接受零到两个参数。
- 如果只提供一个参数,海龟会将其轮廓和填充颜色都更改为作为参数提供的颜色。
- 如果提供两个参数,第一个参数将是海龟的轮廓颜色,第二个将是海龟的填充颜色。
但是得到这个:
- 如果未提供任何参数,
turtle.color()
方法将 return 乌龟的当前颜色以元组的形式显示。
因为你的乌龟的名字是 jack
,jack.color()[0]
将 return 乌龟的轮廓颜色,jack.color()[1]
将 return 乌龟的填充颜色。
如此简单地改变你的功能:
def blueLine():
jack.color("blue")
def blueFill():
jack.color("blue")
至:
def blueLine():
jack.color("blue", jack.color()[1])
def blueFill():
jack.color(jack.color()[0], "blue")
I want to change either the line or the fill color with a key, but not
both.
简单的答案是调用 pencolor()
和 fillcolor()
而不是 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()
我想用键更改线条或填充颜色,但不能同时更改两者。我试过在逗号前加一个虚拟词,但没用。
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()
turtle.color()
接受零到两个参数。
- 如果只提供一个参数,海龟会将其轮廓和填充颜色都更改为作为参数提供的颜色。
- 如果提供两个参数,第一个参数将是海龟的轮廓颜色,第二个将是海龟的填充颜色。
但是得到这个:
- 如果未提供任何参数,
turtle.color()
方法将 return 乌龟的当前颜色以元组的形式显示。
因为你的乌龟的名字是 jack
,jack.color()[0]
将 return 乌龟的轮廓颜色,jack.color()[1]
将 return 乌龟的填充颜色。
如此简单地改变你的功能:
def blueLine():
jack.color("blue")
def blueFill():
jack.color("blue")
至:
def blueLine():
jack.color("blue", jack.color()[1])
def blueFill():
jack.color(jack.color()[0], "blue")
I want to change either the line or the fill color with a key, but not both.
简单的答案是调用 pencolor()
和 fillcolor()
而不是 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()