简体   繁体   English

我正在创建自己的课程。 _Screen.__init__() 采用 1 个位置参数但给出了 4 个类型错误。 我究竟做错了什么?

[英]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.

相关问题 我为什么得到__init __()需要1个位置参数但是给出了2个? - Why am i getting __init__() takes 1 positional argument but 2 were given? 在TKInter中为什么会出现错误-接受1个位置参数,但给出了2个 - in TKInter Why am I getting an error - takes 1 positional argument but 2 were given 在 python 3.7 中创建数组时,我收到一个错误 - (array() 需要 0 个位置 arguments 但给出了 2 个) - I am getting an error - (array() takes 0 positional arguments but 2 were given) while creating an array in python 3.7 __init__() 接受 1 个位置参数,但给定 2 个类型错误 - __init__() takes 1 positional argument but 2 were given type error 调用 OOP 函数时,TypeError: 接受 1 个位置参数,但给出了 2 个。 我该如何解决? - When calling an OOP function, TypeError: takes 1 positional argument but 2 were given. How do I fix this? 类型错误:__init__() 需要 1 到 2 个位置参数,但给出了 3 个。 尝试为我的 BMI 计算器构建 GUI - TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given. Trying to build a GUI for my BMI calculator TypeError: method() 接受 1 个位置参数,但给出了 4 个。 [使用 tkinter] - TypeError: method() takes 1 positional argument but 4 were given. [using tkinter] 图 Class: TypeError: __init__() 采用 1 个位置参数,但给出了 3 个 - Graph Class: TypeError: __init__() takes 1 positional argument but 3 were given TypeError: __init__() 接受 1 个位置参数,但给出了 2 个 — 由 numPy 创建堆栈 class 时出现错误 - TypeError: __init__() takes 1 positional argument but 2 were given — errors comes up when creating stack class by numPy 我在做什么错我收到错误:HiScoresList.sort(key=(BestScore)) TypeError: BestScore() missing 1 required positional argument: 'b' - WHAT AM I DOING WRONG i get the error :HiScoresList.sort(key=(BestScore)) TypeError: BestScore() missing 1 required positional argument: 'b'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM