[英]I am creating my own class. There's a type error that _Screen.__init__() takes 1 positional argument but 4 were given. What am I doing wrong?
# Set up the screen
import turtle
class Screen(turtle.Screen()):
# Create and define an object
def __init__(self, title, bgcolor, height, width, tracer = 0):
turtle.Screen().__init__(self)
# Assign attribute to our instance
self.title = title
self.bgcolor = bgcolor
self.height = height
self.width = width
self.tracer = tracer
Your issue is that you are trying to inherit from a class instance rather than from a class.您的问题是您试图从类实例而不是类继承。 Note that when you are doing请注意,当你在做
class Screen(turtle.Screen()):
you are creating a new instance of the turtle._Screen
class.您正在创建turtle._Screen
类的新实例。
To fix this error, you just have to inherit from the right class as such:要修复此错误,您只需从正确的类继承:
class Screen(turtle._Screen):
# Create and define an object
def __init__(self, title, bgcolor, height, width, tracer = 0):
# Note that here I use super() to access the parent class instead of creating a new instance with turtle.Screen()
super().__init__(self)
# Assign attribute to our instance
self.title = title
self.bgcolor = bgcolor
self.height = height
self.width = width
self.tracer = tracer
Hope this helps you希望这可以帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.