Make Python turtle window size same as canvas size

Viewed 2384

I'm drawing the Syrian flag with Python turtle. I left out the two green stars for now, as they require an external dependency (oh wow, how's that for an accidental political pun? lol). Here's the code:

from turtle import *

# Set up the drawing environment
length = 600
height = 400
setup(length, height)
setworldcoordinates(0,0,length,height)
goto(0,0)
colours = ["red","black","white","Darkgreen"]
bgcolor(colours[0])
speed('fastest')

## Black Stripe ##

pencolor(colours[1])
pendown()
fillcolor(colours[1])
begin_fill()
goto(length,0)
goto(length,height/3)
goto(0,height/3)
goto(0,0)
end_fill()
penup()

# White Stripe

goto(0,height/3)
pencolor(colours[2])
pendown()
fillcolor(colours[2])
begin_fill()
goto(length,height/3)
goto(length,(height/3)*2)
goto(0,(height/3)*2)
goto(0,height/3)
end_fill()
penup()

ht()
done()

Why is there a red border around the image? I have set the canvas size and the coordinate system to equal each other so that 1 pixel equals 1 pixel. The black box, for example, should start at the bottom-left edge of the canvas as I have told it to, but in it's stead lies a red border. If you resize the window manually after running the code, no matter how small you make it in an attempt to overlap the red border, there will always be a red border. I haven't even written a border into the code.

I think it might have something to do with the innards of Python turtle, that it just allows a small border in order to see what lies on the coordinates (0,0), but I can't find any documentation that actually shows how turtle works, they all just say things like "setup(x,y) = set canvas size" or bgcolor("enter colour here") and don't explain any of the definitions that make up the functions.

Does anyone know how to either:

  1. Remove the border?
  2. Make window size equal to canvas size, hence covering the red border?

Cheers!

(edit: By the way, screensize(length,height) gives me red borders and scrollbars.)

1 Answers

Not sure if i can alter it unless i make a tk window from scratch which defeats the purpose of using turtle in the first place.

You can create your window structure with tkinter and then embed a turtle canvas within it. Below is an example of your program done this way, in which you can switch between turtle's ScrolledCanvas and tkiner's plain `Canvas' to see the difference:

import tkinter as tk
from turtle import TurtleScreen, RawTurtle, ScrolledCanvas

WIDTH, HEIGHT = 640, 480

root = tk.Tk()

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT)
# canvas = ScrolledCanvas(root, width=WIDTH, height=HEIGHT)
canvas.pack()

screen = TurtleScreen(canvas)
screen.setworldcoordinates(0, 0, WIDTH, HEIGHT)
screen.bgcolor('red')

turtle = RawTurtle(screen, visible=False)
turtle.speed('fastest')

## Black Stripe ##

turtle.color('black')

turtle.begin_fill()
turtle.setx(WIDTH)
turtle.sety(HEIGHT / 3)
turtle.setx(0)
turtle.sety(0)
turtle.end_fill()

## White Stripe ##

turtle.penup()
turtle.sety(HEIGHT / 3)
turtle.pendown()

turtle.color('white')

turtle.begin_fill()
turtle.setx(WIDTH)
turtle.sety(2 * HEIGHT / 3)
turtle.setx(0)
turtle.sety(HEIGHT / 3)
turtle.end_fill()

screen.mainloop()

The ScrolledCanvas introduces more chrome (borders and other overhead) which shifts things around, sometimes in undesireable ways. Here's a vertical slice showing the difference between plain Canvas on the left and ScrolledCanvas on the right:

enter image description here

as more chrome is added to the window, the drawing area moves slightly. Though the large amount of offset you're seeing in your output that partially disappears when you move the window certainly seems a bug.

Related