Is turtle.Screen a subclass of turtle.TurtleScreen in python? - python

from turtle import Turtle, Screen, TurtleScreen
turtle = Turtle()
screen = Screen()
print(isinstance(turtle, Turtle))
print(isinstance(screen, TurtleScreen))
print(issubclass(Screen,TurtleScreen))
print(isinstance(screen, Screen))
The last 2 prints got some error, debugging says "Screen" is not a class. But from documentation it says "Screen is a subclass of TurtleScreen". So what's wrong here? Is it related to something called Singolton object?

There is no class called Screen. There is a (standalone turtle) function called Screen that returns the singleton TurtleScreen instance. So this is correct:
screen = Screen()
print(isinstance(screen, TurtleScreen))
(The hidden reality is that it is an instance of class _Screen which is a subclass of class TurtleScreen which is a subclass of TurtleScreenBase)
For simplicity, we pretend that we are getting an instance of class Screen that we invoke methods on:
from turtle import Screen
screen = Screen()
screen.setup(800, 600)
But that's a convenient fiction.

Related

Question about turtle module and Turtle() class [duplicate]

How are turtle and Turtle different from each other in python version 2.7?
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:
import turtle
star = turtle.Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = turtle.Screen() # access sole screen instance
screen.mainloop() # screen instance method
and this:
import turtle
for i in range(5):
turtle.forward(50) # function, default turtle
turtle.right(144)
turtle.done() # function, mainloop() synonym, acts on singular screen instance
are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:
from turtle import Turtle, Screen
This forces the object approach to using turtle, making the functional approach unavailable:
from turtle import Turtle, Screen
star = Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = Screen() # access sole screen instance
screen.mainloop() # screen instance method
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.
turtle is the name of the package while Turtle is the name of the class.
An alternate way of importing the module would be:
import turtle.Turtle
Also, are you sure the last line is turtle.done() and not star.done()?
The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.
turtle is a method which contains a class which is noble Turtle

Why is turtle.done() saying it has no attribute 'done'?

I'm using the turtle module. My goal is to keep the window open, and I have heard to use turtle.done() to do so. So far, I've always got this error message:
AttributeError: type object 'Turtle' has no attribute 'done'. Did you mean: 'clone'?
Here is my code.
import turtle
from turtle import Turtle
turtle = Turtle()
turtle.forward(100)
turtle.done()
You shouldn't have a variable name the same as a module. Change the object name from turtle to t, for example, and it'll work:
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
done is a function in the turtle module.
By writing turtle = Turtle() you overwrote the name turtle so that it no longer refers to the module, but to an instance of the class Turtle which does in fact not have a method called done.
The simplest solution would be to choose a different variable name for the Turtle instance.
done is a module-level function, not a method on the Turtle object. Import it from the turtle module on the same line where you import the Turtle class:
from turtle import Turtle, done
turtle = Turtle()
turtle.forward(100)
done()
or import the entire module as turtle (in which case you should not reuse the name turtle for one of your own variables):
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()

Pycharm bug when linking turtle module with separate files

The issue begins when I try to link my modules/files, the turtle class does not instantiate objects to their proper values. I tried changing paths for my python interpreter, but that didn't work. The funny thing is, though- the turtle class works fine with only one module, meaning the object from the class Turtle receives the correct data I give to instantiate it: shape, size, color, etc.
# player.py
from turtle import Turtle
class Player(Turtle):
def __int__(self):
super().__init__()
self.shape('turtle')
screen.exitonclick()
#***********************
# main.py
from player import Player
from turtle import Screen
screen = Screen()
player = Player()
screen.exitonclick()

How do I turn the turtle module along with my mouse

I've been wondering how the turtle would turn with your mouse (like many io games)
Is there a command or something?
Here is how:
import turtle
wn = turtle.Screen() # Create screen
t = turtle.Turtle('turtle') # Create turtle
def drag(x, y): # Define drag function
t.ondrag(0)
t.setheading(t.towards(x, y))
t.goto(x, y)
t.ondrag(drag)
t.ondrag(drag) # Use function on turtle
wn.mainloop()
Output:
EDIT: As pointed out by #cdlane in the comments, the 0 in the t.ondrag method should optimally be None, as the former defines a new event function that throws an exception that isn't seen as it's protected by except Exception: pass.

What is the difference between turtle and Turtle?

How are turtle and Turtle different from each other in python version 2.7?
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:
import turtle
star = turtle.Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = turtle.Screen() # access sole screen instance
screen.mainloop() # screen instance method
and this:
import turtle
for i in range(5):
turtle.forward(50) # function, default turtle
turtle.right(144)
turtle.done() # function, mainloop() synonym, acts on singular screen instance
are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:
from turtle import Turtle, Screen
This forces the object approach to using turtle, making the functional approach unavailable:
from turtle import Turtle, Screen
star = Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = Screen() # access sole screen instance
screen.mainloop() # screen instance method
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.
turtle is the name of the package while Turtle is the name of the class.
An alternate way of importing the module would be:
import turtle.Turtle
Also, are you sure the last line is turtle.done() and not star.done()?
The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.
turtle is a method which contains a class which is noble Turtle

Categories

Resources