Python Forum
Turtle Graphics - Screen & Window Sizing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Turtle Graphics - Screen & Window Sizing (/thread-39352.html)



Turtle Graphics - Screen & Window Sizing - jerryf - Feb-03-2023

I am new to Python but have previous general programming experience. I have Python 3.7 installed on Windows 11 and using Pyscripter as a development tool, display device is 1600 x 900 pixels.

I want to develop simple examples of visualising cartesian based fractal functions that the mixed target audience can see how fractals develop and change some parameters – hence adopting Turtle rather than more technical packages.

I am trying to understand how Screensize(), Setup() and especially Setworldcoordinates() interact when using Turtle graphics.

The following code snippet and outputs illustrate my confusion:
import turtle as tu
tu.setup(400, 400)
tu.screensize(800, 800, bg='lightblue')
tu.setworldcoordinates(-1, -1, 5, 5)  # Custom coordinate system
tu.goto(0, 0)
print("screensize is ",tu.screensize())
print("window size is ",tu.window_width(), tu.window_height())
tu.mainloop()
Output:
*** Remote Interpreter Reinitialized *** screensize is (380, 380) window size is 400 400
Reading the Python documentation I understand the initial screensize() assignment has been reset by setworldcoordinates() but not sure where the (380, 380) is derived from rather than (400,300) default, and can this be changed?

I also understood that setworldcoordinates() acted on the screensize “canvas”. However, in the code above, increasing tu.screensize to (2000,20000) had no effect on the output but then changing tu.setup to (800,800) gave the following output, changing both values.

import turtle as tu
tu.setup(800, 800)
tu.screensize(2000, 2000, bg='lightblue')
tu.setworldcoordinates(-1, -1, 5, 5)  # Custom coordinate system
tu.goto(0, 0)
print("screensize is ",tu.screensize())
print("window size is ",tu.window_width(), tu.window_height())
tu.mainloop()
Output:
*** Remote Interpreter Reinitialized *** screensize is (780, 780) window size is 800 800
Can anyone explain why changing setup() parameters appears to affect both screensize and window size when using setworldcoordinates.


RE: Turtle Graphics - Screen & Window Sizing - jerryf - Feb-09-2023

In response to my own question I think I now know the answer. The following code snippet helps illustrate the answer:

import turtle as tu

#Define size of display window
tu.setup(800, 800)

#Define size of drawing screen/canvas and colour
tu.screensize(400, 400, bg='lightblue')

tu.setworldcoordinates(-1, -1, 5, 5)  # Custom coordinate system

print("screensize is ",tu.screensize())
print("window size is ",tu.window_width(), tu.window_height())

tu.mainloop()
The code produced the following output:

Output:
*** Remote Interpreter Reinitialized *** screensize is (780, 780) window size is 800 800 >>>
Changing the tu.screensize() parameters in the above code to 200,200 and leaving the tu-setup() parameters unchanged did not change the output.

Looking at the following code fragment from setworldcoordinates() function in the Turtle graphics module:

  if self.mode() != "world":
            self.mode("world")
        xspan = float(urx - llx)
        yspan = float(ury - lly)
        wx, wy = self._window_size()
        self.screensize(wx-20, wy-20)
The penultimate line above assigns the variables wx and wy to the width and height of the Turtle window as defined under setup() function, NOT the screen (canvas). The line does not change the underlying parameters of the window.

The last line assigns the values of wx and wy, -20 pixels, derived from the window size in the previous line, to change the screensize (canvas).

This would explain why the screensize is always 20 pixels less than the window size and why changes to the screensize are ignored by the setworldcoordinates() function.