在 Python 之后更改乌龟绘图的颜色
Changing Color of a Turtle Drawing Later in Python
我试图使用 turtle 模块为我在 Python 中从事的项目重新创建一个复选框。为此,我设想在制作复选框时海龟的颜色是空的,一旦用户单击该框,它就会改变颜色。我使用了一个变量,但它似乎不起作用。
color = ((0,255,0))
def Options():
global color
opts = turtle.Screen()
opts.setup(800,800)
opts.reset()
opts.title('Options')
turtle.onscreenclick(None)
boxes = turtle.Turtle()
boxes.ht()
boxes.up()
boxes.sety(300)
boxes.write('Character Control', align = 'center', font = ('Verdana', 30, 'normal'))
boxes.goto(-200,250)
boxes.pd()
boxes.begin_fill()
for i in range(4):
boxes.fd(50)
boxes.rt(90)
boxes.color(color)
boxes.end_fill()
boxes.up()
boxes.goto(0,207)
boxes.write('Attracted to food.', align = 'center', font = ('Verdana', 20, 'normal'))
boxes.goto(-200,180)
boxes.pd()
for i in range(4):
boxes.fd(50)
boxes.rt(90)
boxes.up()
boxes.goto(14, 140)
boxes.write('Attracted to poison.', align = 'center', font = ('Verdana', 20, 'normal'))
turtle.listen()
turtle.onscreenclick(checked)
def checked(x,y):
global color
if x in range(-200, -150) and y in range(200,250):
color = (255, 0, 0)
眼前的问题是这个函数:
def checked(x,y):
global color
if x in range(-200, -150) and y in range(200,250):
color = (255, 0, 0)
x
和 y
参数是浮点数,因此永远不会出现在整数范围内。而是尝试:
def checked(x, y):
global color
if -200 < x < -150 and 200 < y < 250:
color = (255, 0, 0)
I imagined that the color of the turtle at the time of making the
checkbox would be nothing and once the user clicked on the box, it
would change color.
当您成功点击一个盒子并重置 color
变量时,它对海龟和盒子都没有影响。它只是设置一个变量。您缺少足够的代码来使这些框执行某些操作。
但是,这是解决问题的错误方法。更简单的方法是让每个复选框成为一只乌龟,并设置它们各自的 onclick()
事件处理程序而不是屏幕的全局事件处理程序。这是一个基于您现有代码的示例:
import turtle
screen = turtle.Screen()
screen.colormode(255)
color = (0, 255, 0)
STAMP_SIZE = 20
box1 = box2 = None
def Options():
global box1, box2
opts = turtle.Screen()
opts.setup(800, 800)
opts.reset()
opts.title('Options')
boxes = turtle.Turtle(visible=False)
boxes.up()
boxes.sety(300)
boxes.write('Character Control', align='center', font=('Verdana', 30, 'normal'))
boxes.up()
box1 = turtle.Turtle('square', visible=False)
box1.shapesize(50 / STAMP_SIZE)
box1.penup()
box1.goto(-200, 225)
box1.color('black', color)
box1.onclick(checked1)
box1.showturtle()
boxes.goto(0, 207)
boxes.write('Attracted to food.', align='center', font=('Verdana', 20, 'normal'))
box2 = turtle.Turtle('square', visible=False)
box2.shapesize(50 / STAMP_SIZE)
box2.penup()
box2.goto(-200, 155)
box2.color('black', 'white')
box2.onclick(checked2)
box2.showturtle()
boxes.goto(14, 140)
boxes.write('Attracted to poison.', align='center', font=('Verdana', 20, 'normal'))
def checked1(x, y):
box2.fillcolor('white')
box1.fillcolor(color)
def checked2(x, y):
box1.fillcolor('white')
box2.fillcolor(color)
Options()
turtle.mainloop()
现在单击其中一个复选框会将其变为绿色,而另一个变为白色。这应该为您提供基础,让复选框按照您的意愿行事。
我试图使用 turtle 模块为我在 Python 中从事的项目重新创建一个复选框。为此,我设想在制作复选框时海龟的颜色是空的,一旦用户单击该框,它就会改变颜色。我使用了一个变量,但它似乎不起作用。
color = ((0,255,0))
def Options():
global color
opts = turtle.Screen()
opts.setup(800,800)
opts.reset()
opts.title('Options')
turtle.onscreenclick(None)
boxes = turtle.Turtle()
boxes.ht()
boxes.up()
boxes.sety(300)
boxes.write('Character Control', align = 'center', font = ('Verdana', 30, 'normal'))
boxes.goto(-200,250)
boxes.pd()
boxes.begin_fill()
for i in range(4):
boxes.fd(50)
boxes.rt(90)
boxes.color(color)
boxes.end_fill()
boxes.up()
boxes.goto(0,207)
boxes.write('Attracted to food.', align = 'center', font = ('Verdana', 20, 'normal'))
boxes.goto(-200,180)
boxes.pd()
for i in range(4):
boxes.fd(50)
boxes.rt(90)
boxes.up()
boxes.goto(14, 140)
boxes.write('Attracted to poison.', align = 'center', font = ('Verdana', 20, 'normal'))
turtle.listen()
turtle.onscreenclick(checked)
def checked(x,y):
global color
if x in range(-200, -150) and y in range(200,250):
color = (255, 0, 0)
眼前的问题是这个函数:
def checked(x,y):
global color
if x in range(-200, -150) and y in range(200,250):
color = (255, 0, 0)
x
和 y
参数是浮点数,因此永远不会出现在整数范围内。而是尝试:
def checked(x, y):
global color
if -200 < x < -150 and 200 < y < 250:
color = (255, 0, 0)
I imagined that the color of the turtle at the time of making the checkbox would be nothing and once the user clicked on the box, it would change color.
当您成功点击一个盒子并重置 color
变量时,它对海龟和盒子都没有影响。它只是设置一个变量。您缺少足够的代码来使这些框执行某些操作。
但是,这是解决问题的错误方法。更简单的方法是让每个复选框成为一只乌龟,并设置它们各自的 onclick()
事件处理程序而不是屏幕的全局事件处理程序。这是一个基于您现有代码的示例:
import turtle
screen = turtle.Screen()
screen.colormode(255)
color = (0, 255, 0)
STAMP_SIZE = 20
box1 = box2 = None
def Options():
global box1, box2
opts = turtle.Screen()
opts.setup(800, 800)
opts.reset()
opts.title('Options')
boxes = turtle.Turtle(visible=False)
boxes.up()
boxes.sety(300)
boxes.write('Character Control', align='center', font=('Verdana', 30, 'normal'))
boxes.up()
box1 = turtle.Turtle('square', visible=False)
box1.shapesize(50 / STAMP_SIZE)
box1.penup()
box1.goto(-200, 225)
box1.color('black', color)
box1.onclick(checked1)
box1.showturtle()
boxes.goto(0, 207)
boxes.write('Attracted to food.', align='center', font=('Verdana', 20, 'normal'))
box2 = turtle.Turtle('square', visible=False)
box2.shapesize(50 / STAMP_SIZE)
box2.penup()
box2.goto(-200, 155)
box2.color('black', 'white')
box2.onclick(checked2)
box2.showturtle()
boxes.goto(14, 140)
boxes.write('Attracted to poison.', align='center', font=('Verdana', 20, 'normal'))
def checked1(x, y):
box2.fillcolor('white')
box1.fillcolor(color)
def checked2(x, y):
box1.fillcolor('white')
box2.fillcolor(color)
Options()
turtle.mainloop()
现在单击其中一个复选框会将其变为绿色,而另一个变为白色。这应该为您提供基础,让复选框按照您的意愿行事。