Turtle Graphics - 检索重叠形状的颜色

Turtle Graphics - Retrieve Color of Overlapping Shapes

我有一颗红星部分刻在一个包含橙色圆圈的紫色方块中。我正在提取用户点击的点的颜色。当我点击正方形内的圆圈时,返回的颜色是紫色,而不是橙色。当我单击正方形内的红色星形部分时,程序也会 returns 紫色。我该如何纠正这个问题?谢谢你。

import turtle

def border(height,color):

    height = float(height)
    length = height *(1.9)
    length = round(length,2)

    # Draws a rectangle.
    turtle.begin_fill()
    turtle.color(color)
    turtle.down()
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.end_fill()

def big_shape(vertices, steps, length):
    turtle.color("red")
    turtle.begin_fill()
    for i in range(vertices):
        turtle.forward(length)
        turtle.right(steps*360.0/vertices)
    turtle.end_fill()

def textbox_click(rawx,rawy):
    turtle.up()
    turtle.setposition(rawx,rawy)
    turtle.down()
    rawy = -rawy
    canvas = turtle.getcanvas()
    canvas.pack(fill="both", expand=True)
    ids = canvas.find_overlapping(rawx, rawy, rawx, rawy)
    if ids: # if list is not empty
        index = ids[0]
        color = canvas.itemcget(index, "fill")
        if color != '':
            print(color.lower())

def getcoordinates():
    turtle.onscreenclick(turtle.goto)
    turtle.onscreenclick(modifyglobalvariables) # Here's the change!
    turtle.onscreenclick(textbox_click)

def modifyglobalvariables(rawx,rawy):
    global xclick
    global yclick
    xclick = int(rawx//1)
    yclick = int(rawy//1)
    print(xclick)
    print(yclick)

def main():
    border(150,"purple") 
    turtle.begin_fill()
    turtle.down()
    turtle.color("purple")
    turtle.up()

    # Creates the big shape

    x1=150
    y1=3
    turtle.setposition(x1,y1) 
    big_shape(5,2,50)
    turtle.begin_fill()
    turtle.down()
    turtle.up()

    # Circle
    x1=70
    y1=-107
    turtle.setposition(x1,y1) 
    turtle.begin_fill()
    turtle.circle(50)
    turtle.color("orange")
    turtle.end_fill()

    getcoordinates()

    turtle.done()
main()

我看到两个问题

首先: 中查看我的代码 - 您必须获取最后一个元素 ids[-1],而不是第一个 ids[0],才能到达最顶层元素.

其次:你将乌龟移动到点击的地方,所以现在乌龟在最上面 - 所以你可以在获得颜色后移动鼠标,然后你仍然可以使用

 index = ids[-1]

或者你必须从末尾获得第二个 ids[-2] 但是你必须检查 ids 是否至少有两个元素

if len(ids) > 1:     # if list has more than only turtle
    index = ids[-2]  # get second from the end

我建议用不同的方法解决这个问题。与其使用 tkinter 基础来查找非活动对象的颜色(从 turtle 的角度来看),我建议您完全在 turtle 中工作并使绘制的对象处于活动状态。我们可以通过让每个绘图都成为一个海龟光标来做到这一点,这样我们就可以点击海龟,并询问它们的颜色,这是一个更简单的问题:

import turtle

def rectangle(height):
    length = height * 2

    turtle.begin_poly()
    for _ in range(2):
        turtle.forward(length)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
    turtle.end_poly()

    return turtle.get_poly()

def star(vertices, steps, length):
    angle = steps * 360.0 / vertices

    turtle.begin_poly()
    for _ in range(vertices):
        turtle.forward(length)
        turtle.right(angle)
    turtle.end_poly()

    return turtle.get_poly()

def circle(radius):
    turtle.begin_poly()
    turtle.circle(radius)
    turtle.end_poly()

    return turtle.get_poly()

def display_color(turtle):
    print(turtle.fillcolor())

def main():
    # Use the "default" turtle to draw the others
    turtle.penup()
    turtle.hideturtle()
    turtle.setheading(90)
    turtle.speed('fastest')

    screen.register_shape('rectangle', rectangle(150))
    screen.register_shape('star', star(5, 2, 50))
    screen.register_shape('circle', circle(50))

    rectangle_turtle = turtle.Turtle('rectangle')
    rectangle_turtle.penup()
    rectangle_turtle.color('purple')
    rectangle_turtle.onclick(lambda x, y: display_color(rectangle_turtle))

    star_turtle = turtle.Turtle('star')
    star_turtle.penup()
    star_turtle.setposition(150, 3)
    star_turtle.color('red')
    star_turtle.onclick(lambda x, y: display_color(star_turtle))

    circle_turtle = turtle.Turtle('circle')
    circle_turtle.penup()
    circle_turtle.setposition(70, -107)
    circle_turtle.color('orange')
    circle_turtle.onclick(lambda x, y: display_color(circle_turtle))

screen = turtle.Screen()

main()

screen.mainloop()

现在您应该可以单击任何填充的彩色区域,并且您会在控制台中看到打印的颜色名称 window。 (点击 window 本身使其激活后。)