Python Turtle 滚动条 [英] Python Turtle Scrollbars
问题描述
有没有人知道如何防止滚动条出现在 Python 中的 Turtle Graphics 窗口中,适用于小窗口尺寸?
Does anyone know how to prevent scrollbars appearing on the Turtle Graphics window in Python for small window sizes please?
以下代码生成下图.对于 600 像素 x 600 像素的屏幕,不会出现滚动条.
The following code produces the image below. For a screen of 600px by 600px the scrollbars do not appear.
import turtle
TURTLE_SIZE = 20
TRIANGLE_SIZE = 120
screen = turtle.Screen()
screen.setup(400, 400)
triangle = turtle.Turtle("triangle")
triangle.shapesize(TRIANGLE_SIZE / TURTLE_SIZE)
triangle.color("pink")
triangle.right(30)][1]][1]
推荐答案
有谁知道如何防止乌龟出现滚动条Python 中的图形窗口
Does anyone know how to prevent scrollbars appearing on the Turtle Graphics window in Python
答案可能是在 tkinter 中使用乌龟 embedded 而不是 standalone.由于独立海龟默认使用 ScrolledCanvas
,而嵌入式海龟允许您使用它,或者只是坚持使用基本的 Canvas
.
The answer is probably to use turtle embedded in tkinter rather than standalone. As standalone turtle uses ScrolledCanvas
by default, whereas embedded turtle allows you to use it, or simply stick with the basic Canvas
.
也就是说,以下是使用独立海龟实现此目的的三种不同方法:
That said, here are three different ways to achieve this using standalone turtle:
1) 只需将方形窗口从 400 x 400 增加到 420 x 420:
1) Simply increase your square window from 400 x 400 to 420 x 420:
screen.setup(420, 420)
这是可行的,因为海龟已经将窗口和画布的默认大小存储在它的全局_CFG
配置字典中:
This works because turtle has stored in it's global _CFG
configuration dictionary default sizes for the window and canvas:
_CFG = {
"width" : 0.5, # Screen
"height" : 0.75,
"canvwidth" : 400,
"canvheight": 300,
# ...
}
在此画布大小以下,会出现滚动条.除了我们可以在 setworldcoordinates()
中看到的窗口 chrome 有一个模糊因素:
Below this canvas size, scrollbars appear. Except there's a fudge factor to account for window chrome which we can see in setworldcoordinates()
:
self.screensize(wx-20, wy-20)
因此,除非画布也重新调整,否则任何 420 x 320 或更大的窗口在默认情况下都不应该获得滚动条.
So any window 420 x 320, or larger, shouldn't get scrollbars by default unless the canvas is also readjusted.
2) 通过 "turtle.cfg"
文件操作 _CFG
字典.与由 turtle.pen()
方法动态创建的人造 _pd
笔字典不同,turtle._CFG
没有运行时用户界面代码>字典,除非我们在引擎盖下探查:
2) Manipulate the _CFG
dictionary via the "turtle.cfg"
file. Unlike the faux _pd
pen dictionary that's created on the fly by the turtle.pen()
method, there is no runtime user interface for the turtle._CFG
dictionary unless we poke around under the hood:
from turtle import Screen, Turtle, _CFG
TURTLE_SIZE = 20
TRIANGLE_SIZE = 120
_CFG.update({"canvwidth": 380, "canvheight": 380}) # 400 - 20
screen = Screen()
screen.setup(400, 400)
triangle = Turtle("triangle")
triangle.shapesize(TRIANGLE_SIZE / TURTLE_SIZE)
triangle.color("pink")
triangle.right(30)
screen.exitonclick()
3) 修补独立海龟的_Root
类的setupcanvas
方法,用通用的Canvas
替换ScrolledCanvas
.这消除了对任何幻数的需要,只需关闭滚动:
3) Patch the setupcanvas
method of standalone turtle's _Root
class to substitute generic Canvas
for ScrolledCanvas
. This eliminates the need for any magic numbers and will simply turn off scrolling:
import tkinter as TK
from turtle import Screen, Turtle, _Root
def setupcanvas(self, width, height, cwidth, cheight):
self._canvas = TK.Canvas(self, width=cwidth, height=cheight)
self._canvas.pack(expand=1, fill="both")
_Root.setupcanvas = setupcanvas
TURTLE_SIZE = 20
TRIANGLE_SIZE = 120
screen = Screen()
screen.setup(400, 400)
# ...
这篇关于Python Turtle 滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!