How to use python superclass and inheritance (turtle graphics example)?
I've been playing around with turtle graphics, trying to better understand objects and I've encountered a little issue. I've learned about the super() function and wanted to use it:
from turtle import Turtle, _Screen, Screen
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = Screen()
tortoise = turtle()
So if I do this, everything works like charm. I get a screen and I can use any Turtle method on my turtle class.
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
wn = window()
This will also work great, I get what I ask: black window of 1000x1000 in a 500x500 box with slide bars. No problem. But, if I want to combine these:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It will display the screen, but I will get an error from the turtle:
AttributeError: '_Screen' object has no attribute '_mode'
I have been looking through the turtle module and found that the Screen() function does this:
def Screen():
"""Return the singleton screen object.
If none exists at the moment, create a new one and return it,
else return the existing one."""
if Turtle._screen is None:
Turtle._screen = _Screen()
return Turtle._screen
So I modified the window class to include this:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
Turtle._screen = Screen()
super().__init__()
#self.setup(500,500)
#self.screensize(1000,1000)
#self.title("Title")
#self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It works like this, I will get a white window, with a round turtle. But if I uncomment the setup, the screensize or the bgcolor part, I will get an error:
AttributeError: 'window' object has no attribute '_tracing'
or
AttributeError: 'window' object has no attribute 'cv'
So I would again have to declare some parameters before the __init__ in the window class, to make it work. It seems there is something I'm missing. Why does the turtle inherit everything and works great, but the _Screen is not initializing with all it's necessary parameters?
python python-3.x turtle-graphics super superclass
add a comment |
I've been playing around with turtle graphics, trying to better understand objects and I've encountered a little issue. I've learned about the super() function and wanted to use it:
from turtle import Turtle, _Screen, Screen
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = Screen()
tortoise = turtle()
So if I do this, everything works like charm. I get a screen and I can use any Turtle method on my turtle class.
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
wn = window()
This will also work great, I get what I ask: black window of 1000x1000 in a 500x500 box with slide bars. No problem. But, if I want to combine these:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It will display the screen, but I will get an error from the turtle:
AttributeError: '_Screen' object has no attribute '_mode'
I have been looking through the turtle module and found that the Screen() function does this:
def Screen():
"""Return the singleton screen object.
If none exists at the moment, create a new one and return it,
else return the existing one."""
if Turtle._screen is None:
Turtle._screen = _Screen()
return Turtle._screen
So I modified the window class to include this:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
Turtle._screen = Screen()
super().__init__()
#self.setup(500,500)
#self.screensize(1000,1000)
#self.title("Title")
#self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It works like this, I will get a white window, with a round turtle. But if I uncomment the setup, the screensize or the bgcolor part, I will get an error:
AttributeError: 'window' object has no attribute '_tracing'
or
AttributeError: 'window' object has no attribute 'cv'
So I would again have to declare some parameters before the __init__ in the window class, to make it work. It seems there is something I'm missing. Why does the turtle inherit everything and works great, but the _Screen is not initializing with all it's necessary parameters?
python python-3.x turtle-graphics super superclass
add a comment |
I've been playing around with turtle graphics, trying to better understand objects and I've encountered a little issue. I've learned about the super() function and wanted to use it:
from turtle import Turtle, _Screen, Screen
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = Screen()
tortoise = turtle()
So if I do this, everything works like charm. I get a screen and I can use any Turtle method on my turtle class.
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
wn = window()
This will also work great, I get what I ask: black window of 1000x1000 in a 500x500 box with slide bars. No problem. But, if I want to combine these:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It will display the screen, but I will get an error from the turtle:
AttributeError: '_Screen' object has no attribute '_mode'
I have been looking through the turtle module and found that the Screen() function does this:
def Screen():
"""Return the singleton screen object.
If none exists at the moment, create a new one and return it,
else return the existing one."""
if Turtle._screen is None:
Turtle._screen = _Screen()
return Turtle._screen
So I modified the window class to include this:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
Turtle._screen = Screen()
super().__init__()
#self.setup(500,500)
#self.screensize(1000,1000)
#self.title("Title")
#self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It works like this, I will get a white window, with a round turtle. But if I uncomment the setup, the screensize or the bgcolor part, I will get an error:
AttributeError: 'window' object has no attribute '_tracing'
or
AttributeError: 'window' object has no attribute 'cv'
So I would again have to declare some parameters before the __init__ in the window class, to make it work. It seems there is something I'm missing. Why does the turtle inherit everything and works great, but the _Screen is not initializing with all it's necessary parameters?
python python-3.x turtle-graphics super superclass
I've been playing around with turtle graphics, trying to better understand objects and I've encountered a little issue. I've learned about the super() function and wanted to use it:
from turtle import Turtle, _Screen, Screen
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = Screen()
tortoise = turtle()
So if I do this, everything works like charm. I get a screen and I can use any Turtle method on my turtle class.
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
wn = window()
This will also work great, I get what I ask: black window of 1000x1000 in a 500x500 box with slide bars. No problem. But, if I want to combine these:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
super().__init__()
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It will display the screen, but I will get an error from the turtle:
AttributeError: '_Screen' object has no attribute '_mode'
I have been looking through the turtle module and found that the Screen() function does this:
def Screen():
"""Return the singleton screen object.
If none exists at the moment, create a new one and return it,
else return the existing one."""
if Turtle._screen is None:
Turtle._screen = _Screen()
return Turtle._screen
So I modified the window class to include this:
from turtle import Turtle, _Screen, Screen
class window(_Screen):
def __init__(self):
Turtle._screen = Screen()
super().__init__()
#self.setup(500,500)
#self.screensize(1000,1000)
#self.title("Title")
#self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = window()
tortoise = turtle()
It works like this, I will get a white window, with a round turtle. But if I uncomment the setup, the screensize or the bgcolor part, I will get an error:
AttributeError: 'window' object has no attribute '_tracing'
or
AttributeError: 'window' object has no attribute 'cv'
So I would again have to declare some parameters before the __init__ in the window class, to make it work. It seems there is something I'm missing. Why does the turtle inherit everything and works great, but the _Screen is not initializing with all it's necessary parameters?
python python-3.x turtle-graphics super superclass
python python-3.x turtle-graphics super superclass
asked Jan 19 at 20:47
Bogdan PrădatuBogdan Prădatu
377
377
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
As you've discovered, the turtle Screen
is an onion of many layers. There are two issues we need to address: the _Screen
class invokes it's super's (TurtleScreen's) initializer in a way that isn't subclass friendly; the Screen()
function is called from lots of places and it hardcodes which class creates the screen. So let's address both issues:
import turtle
class MyScreen(turtle._Screen):
def __init__(self):
super().__init__()
turtle.TurtleScreen.__init__(self, MyScreen._canvas)
self.setup(500, 500)
self.screensize(1000, 1000)
self.title("Title")
self.bgcolor("black")
def MyScreenFunction():
if turtle.Turtle._screen is None:
turtle.Turtle._screen = MyScreen()
return turtle.Turtle._screen
turtle.Screen = MyScreenFunction
class MyTurtle(turtle.Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = turtle.Screen()
tortoise = MyTurtle()
tortoise.color('white')
tortoise.circle(100)
wn.mainloop()
However, there may be a better way to go. Turtle can be used both standalone as above, and embedded in a tkinter program. The embedded approach uses RawTurtle
, TurtleScreen
and optionally Scrolled Canvas
. These classes may be easier to subclass and the better approach might be to build your own equivalent of standalone turtle through embedding turtle in tkinter and subclassing as needed.
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
@BogdanPrădatu, the initializer call to the parent of_Screen
is done in the old Python 2 style instead of a Python 3 call tosuper()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.
– cdlane
Jan 20 at 22:29
add a comment |
After cdlane's answer, I found out that I could make it a little simpler. The Screen()
part could be introduced in the __init__
function:
from turtle import Turtle, TurtleScreen, _Screen
class window(_Screen):
def __init__(self):
super().__init__()
TurtleScreen.__init__(self, window._canvas)
if Turtle._screen is None:
Turtle._screen = self
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
self.color("white")
self.speed(0)
self.circle(50)
wn = window()
tortoise = turtle()
Thanks again, mate!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271262%2fhow-to-use-python-superclass-and-inheritance-turtle-graphics-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you've discovered, the turtle Screen
is an onion of many layers. There are two issues we need to address: the _Screen
class invokes it's super's (TurtleScreen's) initializer in a way that isn't subclass friendly; the Screen()
function is called from lots of places and it hardcodes which class creates the screen. So let's address both issues:
import turtle
class MyScreen(turtle._Screen):
def __init__(self):
super().__init__()
turtle.TurtleScreen.__init__(self, MyScreen._canvas)
self.setup(500, 500)
self.screensize(1000, 1000)
self.title("Title")
self.bgcolor("black")
def MyScreenFunction():
if turtle.Turtle._screen is None:
turtle.Turtle._screen = MyScreen()
return turtle.Turtle._screen
turtle.Screen = MyScreenFunction
class MyTurtle(turtle.Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = turtle.Screen()
tortoise = MyTurtle()
tortoise.color('white')
tortoise.circle(100)
wn.mainloop()
However, there may be a better way to go. Turtle can be used both standalone as above, and embedded in a tkinter program. The embedded approach uses RawTurtle
, TurtleScreen
and optionally Scrolled Canvas
. These classes may be easier to subclass and the better approach might be to build your own equivalent of standalone turtle through embedding turtle in tkinter and subclassing as needed.
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
@BogdanPrădatu, the initializer call to the parent of_Screen
is done in the old Python 2 style instead of a Python 3 call tosuper()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.
– cdlane
Jan 20 at 22:29
add a comment |
As you've discovered, the turtle Screen
is an onion of many layers. There are two issues we need to address: the _Screen
class invokes it's super's (TurtleScreen's) initializer in a way that isn't subclass friendly; the Screen()
function is called from lots of places and it hardcodes which class creates the screen. So let's address both issues:
import turtle
class MyScreen(turtle._Screen):
def __init__(self):
super().__init__()
turtle.TurtleScreen.__init__(self, MyScreen._canvas)
self.setup(500, 500)
self.screensize(1000, 1000)
self.title("Title")
self.bgcolor("black")
def MyScreenFunction():
if turtle.Turtle._screen is None:
turtle.Turtle._screen = MyScreen()
return turtle.Turtle._screen
turtle.Screen = MyScreenFunction
class MyTurtle(turtle.Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = turtle.Screen()
tortoise = MyTurtle()
tortoise.color('white')
tortoise.circle(100)
wn.mainloop()
However, there may be a better way to go. Turtle can be used both standalone as above, and embedded in a tkinter program. The embedded approach uses RawTurtle
, TurtleScreen
and optionally Scrolled Canvas
. These classes may be easier to subclass and the better approach might be to build your own equivalent of standalone turtle through embedding turtle in tkinter and subclassing as needed.
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
@BogdanPrădatu, the initializer call to the parent of_Screen
is done in the old Python 2 style instead of a Python 3 call tosuper()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.
– cdlane
Jan 20 at 22:29
add a comment |
As you've discovered, the turtle Screen
is an onion of many layers. There are two issues we need to address: the _Screen
class invokes it's super's (TurtleScreen's) initializer in a way that isn't subclass friendly; the Screen()
function is called from lots of places and it hardcodes which class creates the screen. So let's address both issues:
import turtle
class MyScreen(turtle._Screen):
def __init__(self):
super().__init__()
turtle.TurtleScreen.__init__(self, MyScreen._canvas)
self.setup(500, 500)
self.screensize(1000, 1000)
self.title("Title")
self.bgcolor("black")
def MyScreenFunction():
if turtle.Turtle._screen is None:
turtle.Turtle._screen = MyScreen()
return turtle.Turtle._screen
turtle.Screen = MyScreenFunction
class MyTurtle(turtle.Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = turtle.Screen()
tortoise = MyTurtle()
tortoise.color('white')
tortoise.circle(100)
wn.mainloop()
However, there may be a better way to go. Turtle can be used both standalone as above, and embedded in a tkinter program. The embedded approach uses RawTurtle
, TurtleScreen
and optionally Scrolled Canvas
. These classes may be easier to subclass and the better approach might be to build your own equivalent of standalone turtle through embedding turtle in tkinter and subclassing as needed.
As you've discovered, the turtle Screen
is an onion of many layers. There are two issues we need to address: the _Screen
class invokes it's super's (TurtleScreen's) initializer in a way that isn't subclass friendly; the Screen()
function is called from lots of places and it hardcodes which class creates the screen. So let's address both issues:
import turtle
class MyScreen(turtle._Screen):
def __init__(self):
super().__init__()
turtle.TurtleScreen.__init__(self, MyScreen._canvas)
self.setup(500, 500)
self.screensize(1000, 1000)
self.title("Title")
self.bgcolor("black")
def MyScreenFunction():
if turtle.Turtle._screen is None:
turtle.Turtle._screen = MyScreen()
return turtle.Turtle._screen
turtle.Screen = MyScreenFunction
class MyTurtle(turtle.Turtle):
def __init__(self):
super().__init__(shape="circle")
wn = turtle.Screen()
tortoise = MyTurtle()
tortoise.color('white')
tortoise.circle(100)
wn.mainloop()
However, there may be a better way to go. Turtle can be used both standalone as above, and embedded in a tkinter program. The embedded approach uses RawTurtle
, TurtleScreen
and optionally Scrolled Canvas
. These classes may be easier to subclass and the better approach might be to build your own equivalent of standalone turtle through embedding turtle in tkinter and subclassing as needed.
edited Jan 20 at 22:25
answered Jan 19 at 22:06
cdlanecdlane
18.3k21144
18.3k21144
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
@BogdanPrădatu, the initializer call to the parent of_Screen
is done in the old Python 2 style instead of a Python 3 call tosuper()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.
– cdlane
Jan 20 at 22:29
add a comment |
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
@BogdanPrădatu, the initializer call to the parent of_Screen
is done in the old Python 2 style instead of a Python 3 call tosuper()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.
– cdlane
Jan 20 at 22:29
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
Thanks, this seems to work. You said the TurtleScreen initializer is called in an unfriendly manner for subclass use, could you please expand a little on that? TurtleScreen.__init__(self, _Screen._canvas): is it because of the _Screen._canvas part? Should have been self._canvas maybe?
– Bogdan Prădatu
Jan 20 at 8:14
@BogdanPrădatu, the initializer call to the parent of
_Screen
is done in the old Python 2 style instead of a Python 3 call to super()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.– cdlane
Jan 20 at 22:29
@BogdanPrădatu, the initializer call to the parent of
_Screen
is done in the old Python 2 style instead of a Python 3 call to super()
. The old style call has information about the object heirarchy hardcoded into it. The new style fixes this. Also, part of the problem may be trying to subclass standalone turtle so I've added a brief discussion of that to the end of my answer.– cdlane
Jan 20 at 22:29
add a comment |
After cdlane's answer, I found out that I could make it a little simpler. The Screen()
part could be introduced in the __init__
function:
from turtle import Turtle, TurtleScreen, _Screen
class window(_Screen):
def __init__(self):
super().__init__()
TurtleScreen.__init__(self, window._canvas)
if Turtle._screen is None:
Turtle._screen = self
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
self.color("white")
self.speed(0)
self.circle(50)
wn = window()
tortoise = turtle()
Thanks again, mate!
add a comment |
After cdlane's answer, I found out that I could make it a little simpler. The Screen()
part could be introduced in the __init__
function:
from turtle import Turtle, TurtleScreen, _Screen
class window(_Screen):
def __init__(self):
super().__init__()
TurtleScreen.__init__(self, window._canvas)
if Turtle._screen is None:
Turtle._screen = self
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
self.color("white")
self.speed(0)
self.circle(50)
wn = window()
tortoise = turtle()
Thanks again, mate!
add a comment |
After cdlane's answer, I found out that I could make it a little simpler. The Screen()
part could be introduced in the __init__
function:
from turtle import Turtle, TurtleScreen, _Screen
class window(_Screen):
def __init__(self):
super().__init__()
TurtleScreen.__init__(self, window._canvas)
if Turtle._screen is None:
Turtle._screen = self
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
self.color("white")
self.speed(0)
self.circle(50)
wn = window()
tortoise = turtle()
Thanks again, mate!
After cdlane's answer, I found out that I could make it a little simpler. The Screen()
part could be introduced in the __init__
function:
from turtle import Turtle, TurtleScreen, _Screen
class window(_Screen):
def __init__(self):
super().__init__()
TurtleScreen.__init__(self, window._canvas)
if Turtle._screen is None:
Turtle._screen = self
self.setup(500,500)
self.screensize(1000,1000)
self.title("Title")
self.bgcolor("black")
class turtle(Turtle):
def __init__(self):
super().__init__(shape="circle")
self.color("white")
self.speed(0)
self.circle(50)
wn = window()
tortoise = turtle()
Thanks again, mate!
answered Jan 20 at 8:21
Bogdan PrădatuBogdan Prădatu
377
377
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271262%2fhow-to-use-python-superclass-and-inheritance-turtle-graphics-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown