2

Does anyone know how to prevent scrollbars appearing on the Turtle Graphics window in Python for small window sizes please?

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]

enter image description here

Robin Andrews
  • 3,514
  • 11
  • 43
  • 111

2 Answers2

2

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)

# ...

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

Edited

Ensuring that the margin value is the smallest as possible will make that the origin of the canva's cartesian plane is as closer as possible to the center of the screen. I have a utility class that I put on the following question that helps adjusting window's properties to figure out the best value for margin

I came across this question while learning about turtle. The following approach worked for me:

from turtle import Turtle

donatello = Turtle("turtle")
donatello.color("purple")  # just to keep it real
screen = donatello.getscreen()
window_width = 200
window_height = 250
margin = 2  # minimum difference between window size and canvas size to avoid scrollbars
screen.screensize(  # despite the name, screensize() does not change window/screen's size
    canvwidth=window_width - margin,  # named parameters give a hint of method's real goal
    canvheight=window_height - margin)  # canvas has to be smaller than window
screen.setup(window_width, window_height)  # setup() will change window/screen's size
print(f"screensize(canvas) = {screen.screensize()}\n"
      f"screen(width*height) = ({screen.window_width()} * {screen.window_height()})")
screen.exitonclick()

output:

screensize(canvas) = (198, 248)
screen(width*height) = (200 * 250)

Basically is just ensuring that the canvas is a few units smaller than the screen dimensions, and just beware of the deceiving screensize() method.

turtle screen without scrollbars

CamelCamelius
  • 333
  • 2
  • 15