为什么 canvas 小乌龟会打开一个更小的屏幕?
Why does turtle open an even smaller screen when the canvas is small?
我正在尝试使用 turtle 在 200x200 的小屏幕上绘图,但是绘图不会以完整尺寸弹出,它会打开一个较小的 window,我必须滚动 up/down ,left/right(一点点)看全图。对于更大的 windows,我没有这个问题。我该如何防止这种情况?
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
编辑:screenshot,我想要实际尺寸 window 而不是卷轴尺寸
您可以将 window 调整为 420×420。
如果您不想调整 window 的大小,我建议修改 turtle._CFG
字典中 "canvwidth"
和 "canvheight"
键的值:
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
turtle._CFG.update({"canvwidth": width-20, "canvheight": height-20}) # Removing the scroll bars
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
screen.exitonclick()
在 turtle 中使用小 windows 是一堆蠕虫。如果@TheOneMusic 的简单解决方案 (+1) 足以满足您的目的,那就去做吧!在我的系统上,您的 setworldcoordinates()
调用去掉了滚动条,所以我什至看不到这个问题。因此,另一个近似的解决方案可能是升级到当前的 Python 和 tkinter。
然而,两者都不是精确解。如果我们添加代码在绘图区域周围绘制一个 200 x 200 的框:
t.penup()
t.color('red')
t.goto(0, 0) # because of setworldcoordinates()
t.pendown()
for _ in range(4):
t.forward(200)
t.left(90)
我们把盒子弄歪了:
为了更准确地解决这个问题,涉及更丑陋的代码:
from turtle import Screen, Turtle
from random import randint
TRUE_WIDTH, TRUE_HEIGHT = 200, 200
CURSOR_SIZE = 20 # for drawing frame around edge
RADIUS = 20
CHROME = 14 # magic number possibly derivable from tkinter
width, height = TRUE_WIDTH + CHROME, TRUE_HEIGHT + CHROME # needs to be slightly larger than 200 target
offset_x = CHROME / -2 + 2
offset_y = CHROME / 2 - 2
screen = Screen()
screen.setup(width, height)
screen.screensize(width/2, height/2) # backing store needs to be smaller than window
screen.setworldcoordinates(0, 0, TRUE_WIDTH, TRUE_HEIGHT)
# Draw red frame around edge to "prove" drawing area
frame = Turtle(shape='square', visible=False)
frame.shapesize(TRUE_HEIGHT / CURSOR_SIZE, TRUE_WIDTH / CURSOR_SIZE) # 200 x 200 frame
frame.color('red', 'white')
frame.penup()
frame.goto(TRUE_WIDTH/2 + offset_x, TRUE_HEIGHT/2 + offset_y)
frame.stamp()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
for _ in range(5):
turtle.penup()
turtle.goto(randint(RADIUS, TRUE_WIDTH - RADIUS) + offset_x, randint(0, TRUE_HEIGHT - RADIUS*2) + offset_y)
turtle.pendown()
turtle.circle(RADIUS)
screen.exitonclick()
但是这种细节工作很容易被 turtle and/or tkinter 的未来版本取消。如果您可以接受 turtle 的默认 window,生活会变得更轻松。
我正在尝试使用 turtle 在 200x200 的小屏幕上绘图,但是绘图不会以完整尺寸弹出,它会打开一个较小的 window,我必须滚动 up/down ,left/right(一点点)看全图。对于更大的 windows,我没有这个问题。我该如何防止这种情况?
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
编辑:screenshot,我想要实际尺寸 window 而不是卷轴尺寸
您可以将 window 调整为 420×420。
如果您不想调整 window 的大小,我建议修改 turtle._CFG
字典中 "canvwidth"
和 "canvheight"
键的值:
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
turtle._CFG.update({"canvwidth": width-20, "canvheight": height-20}) # Removing the scroll bars
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
screen.exitonclick()
在 turtle 中使用小 windows 是一堆蠕虫。如果@TheOneMusic 的简单解决方案 (+1) 足以满足您的目的,那就去做吧!在我的系统上,您的 setworldcoordinates()
调用去掉了滚动条,所以我什至看不到这个问题。因此,另一个近似的解决方案可能是升级到当前的 Python 和 tkinter。
然而,两者都不是精确解。如果我们添加代码在绘图区域周围绘制一个 200 x 200 的框:
t.penup()
t.color('red')
t.goto(0, 0) # because of setworldcoordinates()
t.pendown()
for _ in range(4):
t.forward(200)
t.left(90)
我们把盒子弄歪了:
为了更准确地解决这个问题,涉及更丑陋的代码:
from turtle import Screen, Turtle
from random import randint
TRUE_WIDTH, TRUE_HEIGHT = 200, 200
CURSOR_SIZE = 20 # for drawing frame around edge
RADIUS = 20
CHROME = 14 # magic number possibly derivable from tkinter
width, height = TRUE_WIDTH + CHROME, TRUE_HEIGHT + CHROME # needs to be slightly larger than 200 target
offset_x = CHROME / -2 + 2
offset_y = CHROME / 2 - 2
screen = Screen()
screen.setup(width, height)
screen.screensize(width/2, height/2) # backing store needs to be smaller than window
screen.setworldcoordinates(0, 0, TRUE_WIDTH, TRUE_HEIGHT)
# Draw red frame around edge to "prove" drawing area
frame = Turtle(shape='square', visible=False)
frame.shapesize(TRUE_HEIGHT / CURSOR_SIZE, TRUE_WIDTH / CURSOR_SIZE) # 200 x 200 frame
frame.color('red', 'white')
frame.penup()
frame.goto(TRUE_WIDTH/2 + offset_x, TRUE_HEIGHT/2 + offset_y)
frame.stamp()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
for _ in range(5):
turtle.penup()
turtle.goto(randint(RADIUS, TRUE_WIDTH - RADIUS) + offset_x, randint(0, TRUE_HEIGHT - RADIUS*2) + offset_y)
turtle.pendown()
turtle.circle(RADIUS)
screen.exitonclick()
但是这种细节工作很容易被 turtle and/or tkinter 的未来版本取消。如果您可以接受 turtle 的默认 window,生活会变得更轻松。