对于这个问题,我有两个解决方案,我已经在各种程序中使用过。
第一个是您的邮票解决方案的变体。不要使用screen.register_shape() 为每条线注册一个自定义多边形,而是使用方形海龟并将每条线turtle.turtlesize() 它放入您要标记的矩形中:
from turtle import Turtle, Screen
STAMP_SIZE = 20 # size of the square turtle shape
WIDTH, LENGTH = 25, 125
yertle = Turtle(shape="square")
yertle.penup()
yertle.turtlesize(WIDTH / STAMP_SIZE, LENGTH / STAMP_SIZE)
yertle.goto(100 + LENGTH//2, 100) # stamps are centered, so adjust X
yertle.stamp()
screen = Screen()
screen.exitonclick()
当我需要 draw 而不是 stamp 时,我的另一个解决方案是进入海龟的 tkinter 基础并修改海龟的硬编码线端形状本身:
from turtle import Turtle, Screen
import tkinter as _
_.ROUND = _.BUTT
WIDTH, LENGTH = 25, 125
yertle = Turtle()
yertle.width(WIDTH)
yertle.penup()
yertle.goto(100, 100)
yertle.pendown()
yertle.forward(LENGTH)
screen = Screen()
screen.exitonclick()