Python turtle ondrag event vs. compound shapes

此生再无相见时 提交于 2020-06-15 04:13:05

问题


When I register a polygon, or a compound shape with only a single component, I'm able to create a turtle cursor using that shape, add a drag event handler, and drag it around the screen.

But when I register a compound shape with a second component, I can no longer drag it:

from turtle import Turtle, Screen, Shape

def simple_polygon(turtle):

    turtle.begin_poly()
    turtle.circle(50)
    turtle.end_poly()
    screen.register_shape("simple_polygon", turtle.get_poly())

    turtle.reset()

def compound_single(turtle):

    shape = Shape("compound")

    turtle.begin_poly()
    turtle.circle(50)
    turtle.end_poly()
    shape.addcomponent(turtle.get_poly(), "blue", "blue")  # component #1
    screen.register_shape("compound_single", shape)

    turtle.reset()

def compound_double(turtle):

    shape = Shape("compound")

    turtle.begin_poly()
    turtle.circle(50)
    turtle.end_poly()
    shape.addcomponent(turtle.get_poly(), "green", "green")  # component #1

    turtle.penup()
    turtle.left(90)
    turtle.forward(25)
    turtle.right(90)
    turtle.pendown()

    turtle.begin_poly()
    turtle.circle(25)
    turtle.end_poly()
    shape.addcomponent(turtle.get_poly(), "yellow", "yellow")  # component #2
    screen.register_shape("compound_double", shape)

    turtle.reset()

def drag_handler(turtle, x, y):
    turtle.ondrag(None)  # disable ondrag event inside drag_handler
    turtle.goto(x, y)
    turtle.ondrag(lambda x, y, turtle=turtle: drag_handler(turtle, x, y))

screen = Screen()

magic_marker = Turtle()
simple_polygon(magic_marker)
compound_single(magic_marker)
compound_double(magic_marker)
magic_marker.hideturtle()

red = Turtle(shape="simple_polygon")
red.color("red")
red.penup()
red.goto(150, 150)
red.ondrag(lambda x, y: drag_handler(red, x, y))

blue = Turtle(shape="compound_single")
blue.penup()
blue.goto(-150, -150)
blue.ondrag(lambda x, y: drag_handler(blue, x, y))

mostly_green = Turtle(shape="compound_double")
mostly_green.penup()
mostly_green.goto(150, -150)
mostly_green.ondrag(lambda x, y: drag_handler(mostly_green, x, y))

screen.mainloop()

You'll find that only two of the three shapes generated can be dragged. Comment out this line:

shape.addcomponent(turtle.get_poly(), "yellow", "yellow")  # component #2

and the third circle will be all green and become draggable.

I can't find any mention in the turtle documentation about compound shapes with mutiple components not being valid cursors as far as dragging. It doesn't make any difference whether the second component is completely within, overlapping or outside the first.

Looking at the turtle code, I see no distinction, leading me to believe that this problem is in the tkinter underpinning and not documented properly in turtle. Is this problem Unix or OSX specific?

Am I missing something? Why can't I drag cursors built out of multiple components?


回答1:


I think what's going on is when you drag one object, the whole canvas moves and it looks like it's moving.

The same thing happens in Canvas Tkinter. When you program something to happen when you click an object, the whole canvas is waiting for a click. SO that means if two objects have a onclick event, the whole thing won't work.

Hope this helps!!



来源:https://stackoverflow.com/questions/43176331/python-turtle-ondrag-event-vs-compound-shapes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!