drag-and-dropshareturtle-graphicspoly

Is it possible to change the pensize of a drag-able poly in turtle?


I'm trying to change the pensize of a drag-able poly in turtle, so that the poly would have a broad border around it?

Here is part of the code:

from turtle import Turtle,Shape,Screen

def simple_polygon(turtle):

    shape = Shape("compound")
    turtle.begin_poly()
    turtle.circle(50)
    shape.addcomponent(turtle.get_poly(), "yellow", "green")  # component #2
    screen.register_shape("simple_polygon", 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)
magic_marker.hideturtle()

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

screen.mainloop()

Can someone show me how it's done?


Solution

  • Is it possible to change the pensize of a drag-able poly in turtle, so that the poly would have a broad border around it?

    Yes it is. Not at polygon creation nor registration time, but via the outline argument to shapesize() (aka turtlesize()) once it has been set as the turtle cursor:

    from turtle import Screen, Turtle
    
    def drag_handler(x, y):
        turtle.ondrag(None)  # disable event inside handler
        turtle.goto(x, y)
        turtle.ondrag(drag_handler)
    
    screen = Screen()
    
    turtle = Turtle()
    turtle.begin_poly()
    turtle.circle(50)
    turtle.end_poly()
    screen.register_shape('simple_polygon', turtle.get_poly())
    turtle.reset()
    
    turtle.shape('simple_polygon')
    turtle.color('green', 'yellow')
    turtle.shapesize(outline=25)
    turtle.penup()
    
    turtle.ondrag(drag_handler)
    
    screen.mainloop()
    

    This is not an answer to the question cited, as compound turtles take on many forms, and are not draggable. But it is a useful thing to do:

    enter image description here