稍后在Python中更改海龟图形的颜色

稍后在Python中更改海龟图形的颜色,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我试图使用turtle模块为我在Python中工作的项目重新创建一个复选框。为此,我设想在创建复选框时海龟的颜色将为零,一旦用户单击该框,它将改变颜色。我使用了一个变量,但它似乎不起作用 color = ((0,255,0)) def Options(): global color opts = turtle.Screen() opts.setup(800,800) opts.reset() opts.title('Options') turtle

我试图使用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)

现在单击其中一个复选框将使其变为绿色,另一个变为白色。这将为您提供基础,使复选框满足您的需要。

谢谢您的解决方案。我想知道是否可以同时选中这两个复选框。并不是说你需要花时间在这上面@cdlane@rbhog,是的,您可以让它们成为独立的复选框,只需更改
checked1()
checked2()
事件处理程序中的逻辑即可。例如,让
checked1()
查看
box1
的填充颜色,然后切换到另一个状态。这就是我认为我需要做的。
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()