为什么在画布很小的情况下,turtle 会打开更小的屏幕? [英] Why does turtle open an even smaller screen when the canvas is small?
问题描述
我正在尝试使用乌龟在一个 200x200 的小屏幕上绘图,但是绘图没有以全尺寸弹出,它打开了一个较小的窗口,我必须向上/向下、向左/向右滚动(只是一点点) 查看整个图纸.我在大窗户上没有这个问题.我如何防止这种情况发生?
导入海龟随机导入高度,宽度 = 200, 200屏幕 = 乌龟.屏幕()screen.setup(宽度,高度)screen.setworldcoordinates(0, 0, width, height)t = 乌龟.乌龟()t.speed(1)对于 _ 范围(5):t.penup()t.goto(random.randint(20, width-20), random.randint(0, height-40))t.pendown()t.circle(20)
为了更准确地解决这个问题,涉及到更丑的代码:
from turtle import Screen, Turtle来自随机导入 randintTRUE_WIDTH, TRUE_HEIGHT = 200, 200CURSOR_SIZE = 20 # 用于在边缘周围绘制框架半径 = 20CHROME = 14 # 可能源自 tkinter 的幻数width, height = TRUE_WIDTH + CHROME, TRUE_HEIGHT + CHROME # 需要略大于200目标offset_x = 铬/-2 + 2offset_y = 铬/2 - 2屏幕 = 屏幕()screen.setup(宽度,高度)screen.screensize(width/2, height/2) # 后备存储需要小于窗口screen.setworldcoordinates(0, 0, TRUE_WIDTH, TRUE_HEIGHT)# 在边缘周围画红框以证明"绘图区域框架 = 海龟(形状 = 方形,可见 = 假)frame.shapesize(TRUE_HEIGHT/CURSOR_SIZE, TRUE_WIDTH/CURSOR_SIZE) # 200 x 200 帧frame.color('红色', '白色')frame.penup()frame.goto(TRUE_WIDTH/2 + offset_x, TRUE_HEIGHT/2 + offset_y)frame.stamp()乌龟 = 乌龟()turtle.speed('fastest') # 因为我没有耐心对于 _ 范围(5):乌龟.penup()龟.goto(randint(RADIUS, TRUE_WIDTH - RADIUS) + offset_x, randint(0, TRUE_HEIGHT - RADIUS*2) + offset_y)乌龟.pendown()乌龟.圆(半径)screen.exitonclick()
但是这种细节工作很容易被乌龟和/或 tkinter 的未来版本取消.如果你能忍受乌龟的默认窗口,生活就会变得更轻松.
I'm trying to draw on a small 200x200 screen using turtle, however the drawing doesn't pop up as full size, it opens a smaller window and I have to scroll up/down, left/right (just a bit) to see the whole drawing. I don't have this problem with larger windows. How do I prevent this?
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)
edit: screenshot, I want the actual size window instead of the scrolls
Using small windows in turtle is a can of worms. If @TheOneMusic's simple solution (+1) is good enough for your purposes, go for it! On my system, your setworldcoordinates()
call gets rid of the scroll bars, so I don't even see the issue. So, another approximate solution might be to upgrade to current Python and tkinter.
However, neither is an exact solution. If we add code to draw a 200 x 200 box around our drawing area:
t.penup()
t.color('red')
t.goto(0, 0) # because of setworldcoordinates()
t.pendown()
for _ in range(4):
t.forward(200)
t.left(90)
We get the box skewed:
To solve this problem more precisely, involves uglier code:
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()
But this sort of detail work could easily be undone by a future release of turtle and/or tkinter. If you can live with turtle's default window, life gets easier.
这篇关于为什么在画布很小的情况下,turtle 会打开更小的屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!