Does anyone know how to prevent scrollbars appearing on the Turtle
Graphics window in Python
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) Simply increase your square window from 400 x 400 to 420 x 420:
screen.setup(420, 420)
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,
# ...
}
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)
So any window 420 x 320, or larger, shouldn't get scrollbars by default unless the canvas is also readjusted.
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) 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)
# ...
