Python Forum
Turtle Graphics - Screen & Window Sizing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Graphics - Screen & Window Sizing
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,613 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 1,511 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,132 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Python graphics kaltenherz 1 1,683 Sep-05-2021, 05:19 PM
Last Post: jefsummers
  Python 3 Turtle - Screen Events and Coords peteralien 0 1,652 Aug-18-2020, 11:25 PM
Last Post: peteralien
  Turtle python graphics Y0sh1 6 3,384 Jun-10-2020, 10:05 AM
Last Post: DPaul
  turtle window not showing yokeloonlo01 3 7,784 May-29-2020, 09:41 AM
Last Post: pyzyx3qwerty
  turtle.Screen() not working Jdawgg531 0 2,696 May-04-2020, 12:43 AM
Last Post: Jdawgg531
  Start a full screen window marted 4 3,811 Nov-14-2019, 11:03 AM
Last Post: marted
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,129 Feb-06-2019, 01:25 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020