Python 乌龟 - 矩形不适合相同大小的 window

Python Turtle - Rectangle won't fit in window with same size

我想在乌龟屏幕上画一个与屏幕大小相同的矩形,但它越过了边界(只有当我放大时才可见window)但我需要它全部显示在屏幕上。

That's how the screen looks

代码片段:

WIDTH, HEIGHT = 280, 207

turtle = t.Turtle()
turtle.hideturtle()
turtle.speed(0)
turtle.penup()

turtle_screen = turtle.getscreen()
# coords should be in upper left corner
turtle_screen.setup(width = WIDTH + 20, height = HEIGHT + 20, startx = None, starty = None)
turtle_screen.setworldcoordinates(llx = -1, lly = WIDTH, urx = HEIGHT, ury = -1)

turtle.penup()
turtle.setpos(0, 0)
turtle.color('black', '#dbb4a0')
turtle.pendown()
turtle.begin_fill()
edges = [ WIDTH, HEIGHT, WIDTH, HEIGHT]
for edge in edges:
    turtle.forward(edge)
    turtle.left(90)

turtle.end_fill()
turtle_screen.exitonclick()

我想知道内屏的坐标是否与尺寸不同?有什么想法吗?

这是您更正的代码:

import turtle as t

WIDTH, HEIGHT = 280, 207

turtle = t.Turtle()
turtle.hideturtle()
turtle.speed(0)
turtle.penup()

turtle_screen = turtle.getscreen()
# coords should be in upper left corner
turtle_screen.setup(width = WIDTH + 20, height = HEIGHT + 20, startx = None, starty = None)
turtle_screen.setworldcoordinates(llx = 0, lly = HEIGHT, urx = WIDTH, ury = 0)

turtle.penup()
turtle.setpos(0, 0)
turtle.color('black', '#dbb4a0')
turtle.pendown()
turtle.begin_fill()
edges = [ WIDTH, HEIGHT, WIDTH, HEIGHT]
for edge in edges:
    turtle.forward(edge)
    turtle.left(90)

turtle.end_fill()
turtle_screen.exitonclick()

您刚刚在 setworldcoordinates 调用中反转了 llyurx。 X轴对应你的WIDTH,y轴对应你的HEIGHT

这是我的类似解决方案(还纠正了 setworldcoordinates() 中的 HEIGHTWIDTH 倒置)但基于我之前关于 turtle 中的 的回答:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 280, 207

CHROME = 14
OFFSET = CHROME / -2 + 2

screen = Screen()
# coords should be in upper left corner
screen.setup(WIDTH + CHROME, HEIGHT + CHROME)
screen.setworldcoordinates(llx=0, lly=HEIGHT, urx=WIDTH, ury=0)

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.color('black', '#dbb4a0')

turtle.penup()
turtle.setpos(OFFSET, OFFSET)
turtle.pendown()

turtle.begin_fill()

for _ in range(2):
    turtle.forward(WIDTH)
    turtle.left(90)
    turtle.forward(HEIGHT)
    turtle.left(90)

turtle.end_fill()

screen.exitonclick()