What does turtle.Screen() actually do?

Question:

I decided to create a small game with interface created with only turtle, but I can’t understand what turtle.Screen() actually does. I searched the internet and didn’t find an answer to my question anywhere except for docs, but the explanation is unclear to me.

I want somebody to explain me why I need to initialize a var with turtle.Screen() object. I understood that it is not a class, but it tells me only that "it is not a class".

I experimented with that a bit and didn’t find any need to use it. Only if I want to get all the turtle.Turtle()s from the current screen, but I also can’t see any practical application to this.

I thought, "Maybe it creates another screen", but this program:

import turtle
sc1 = turtle.Screen()
turtle.forward(100)
sc2 = turtle.Screen()
turtle.backward(100)
turtle.done()

has only one screen.

I also clean the window through turtle.clear(). If I want to use several brushes (turtles), I can do that without creating a Screen(). If I want to adjust the window size, I can do that without Screen().

So what does turtle.Screen() actually do? Not what it is, but what it does.

Asked By: Beholder

||

Answers:

From the official documentation

The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application.

The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible.

All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.

I agree the architecture of the module can be a bit confusing for newcomers. I will assume you have some basic understanding of OOP, as this module is built on it.

There is a class TurtleScreen, which represents a window for turtles (you can read the details in the doc). You can create an instance of this class yourself using the constructor (needs a tkinter.Canvas or a ScrolledCanvas), which you should do when you use turtle as a part of a bigger project (for ex. adding a turtle canvas to a bigger tkinter app).

There is, however, a convenience method, Screen(). This creates and returns a singleton screen – so it creates the TurtleScreen instance for you (and also the Canvas & tkinter window object). You have way less control over the creation of the Screen this way, so it’s not useful in all scenarios, but it gets the job done if you are happy with the defaults, and only want one screen. The term singleton means that it only creates the TurtleScreen once, so if you call the function more than once, you get back the same object – so you can’t have multiple screens this way.

To be even more confusing convenient, methods of the TurtleScreen (and other classes like Turtle) are also available as functions acting on the singleton instancess – i.e bgcolor() is actually the bgcolor method of Screen bound to the singleton instance, and forward() is also just calling the forward() method of the Turtle classes singleton instance.

This class-singleton-convenience function design can be quite confusing if you don’t understand it’s background, and is not that commonly used.

You can use

  • the convenience functions if you just want to use one object of each class with their "basic settings"
  • the singleton instances if you need a bit more control over them (in case some methods / properties are not "exported" as functions)
  • or you can create your own instances if you need full control or you want to use more than one object.
Answered By: Sasszem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.