2017-11-27 4 views
0

주황색 원을 포함하는 자주색 사각형 안에 부분적으로 새겨진 빨간 별이 있습니다. 사용자가 클릭하는 지점의 색을 추출 중입니다. 사각형 안의 원을 클릭하면 반환되는 색상은 주황색이 아닌 자주색입니다. 광장 안쪽에있는 빨간 별 부분을 클릭하면 프로그램이 자주 나타납니다. 이 문제를 어떻게 해결할 수 있습니까? 고맙습니다.거북이 그래픽 - 겹치는 도형의 색 검색

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() 
+0

을'모든 요소를 ​​겹쳐 유지 ids'. 어쩌면 보라색 사각형이 최상위 요소 일 것입니다. 'ids'에서 다른 값들을 확인할 수 있습니다. – furas

답변

0

나는 두 가지 문제

먼저

참조 : previous question에서 내 코드를 볼 수을 - 당신은 맨 위의 요소를 얻을 수있는 마지막 요소 ids[-1]하지 첫째 ids[0]를 얻을 수 있습니다.

둘째 : 당신이 turle 이제 장소를 클릭하는 거북이를 이동하는 맨 위입니다 - 그래서 당신은 색상을 얻을 후 마우스를 움직일 수하고 여전히

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 
1

나는이 문제에 대한 다른 접근 방식을 제안을 가지고 있는지 확인해야합니다. 거북이의 관점에서 보면 비활성 오브젝트의 색상을 찾기 위해 tkinter 기반을 사용하는 대신 거북이 내에서 완전히 작업하고 그려진 오브젝트를 활성화하는 것이 좋습니다.

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() 

지금 당신이 채워진의 하나를 클릭 할 수 있어야한다 : 우리는 우리가 거북이를 클릭하고 있도록 각 거북이 커서를 그리기 만들기 및 간단한 문제가 자신의 색상을 심문하여이 작업을 수행 할 수 있습니다 콘솔 영역에 인쇄 된 색상의 이름을 볼 수 있습니다. (당신 후에 활성화합니다 창 자체를 클릭하십시오.)

enter image description here