0

我正在尝试更改海龟中可拖动多边形的 pensize,以便多边形周围有一个宽边框?

以下是部分代码:

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

有人可以告诉我它是如何完成的吗?

4

1 回答 1

1

是否可以更改海龟中可拖动多边形的笔大小,以便多边形周围有宽边框?

是的。不是在多边形创建或注册时,而是通过(aka ) 的outline参数,一旦它被设置为海龟光标:shapesize()turtlesize()

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

不是对所引用问题的回答,因为复合海龟具有多种形式,并且不可拖动。但这是一件有用的事情:

在此处输入图像描述

于 2020-05-23T05:02:16.653 回答