__init__() missing 1 required positional argument , but i think i put it?

1.4k views Asked by At

I program an educational game where the user has to reproduce a piece of factory offered by the computer. When I instantiate objects in the Piece class in my Application class and use the drawPiece () method, I have this error:

    Traceback (most recent call last):
  File "/Users/boriselgareh/Downloads/DMV2 5/Application.py", line 32, in <module>
    Application(8).mainloop()
  File "/Users/boriselgareh/Downloads/DMV2 5/Application.py", line 12, in __init__
    self.initPieces()
  File "/Users/boriselgareh/Downloads/DMV2 5/Application.py", line 21, in initPieces
    pieceModele=PieceModele(Canvas())
  File "/Users/boriselgareh/Downloads/DMV2 5/Piece.py", line 82, in __init__
    Piece.__init__(self)
TypeError: __init__() missing 1 required positional argument: 'can'

Here are the two classes that interest us, The Piece and Application classes are contained in separate files :

from VueChaine import *
from Chaine import *
from tkinter import *
from Piece import *

class Application(Tk):
    def __init__(self,nbCotes):
        Tk.__init__(self)
        self.nbCotes=nbCotes
        self.initFenetre()
        self.initPieces()

    def initFenetre(self):
        self.title("l'usine TTT")
        self.chaine=Chaine((3*self.nbCotes-2)//2)
        self.afficheChaine=VueChaine(self,self.nbCotes)
        self.afficheChaine.grid()

    def initPieces(self):
        pieceModele=PieceModele(Canvas(self))
        pieceUsine=PieceUsine(Canvas(self))
        pieceModele.getCan().pack(self)
        pieceUsine.getCan().pack(self)
        pieceUsine.dessinerPieces()
        pieceModele.dessinerPieces()




if __name__=="__main__":
    Application(8).mainloop()

from random import *
from math import *
from tkinter import *

class Piece(object):

    def __init__(self,can,x=100,y=100,r=100,nbCotes=8):
        self.__nbCotes=nbCotes
        self.__can=can
        self.xCentre=x
        self.yCentre=y
        self.rayon=r
        self.generer()

    def dessinerPieces(self):
        sommets=[]
        for i in range(self.getNbCotes()):
            sommets.append([self.xCentre+self.rayon * cos(2 * pi * (i+1/2) / self.getNbCotes()),
                                self.yCentre +self.rayon * sin(2 * pi * (i+1/2) / self.getNbCotes())])

        self.getCan().create_polygon(sommets, outline='gray', fill='gray')
        self.getCan().pack()

        delta=self.rayon-self.rayon*cos(pi/self.getNbCotes())
        angle = (2 * pi) / self.getNbCotes()

        for i in range(self.getNbCotes()//2):
            if self.traits[i] != 0:
                x0 = self.xCentre + (self.rayon - delta) * cos(angle * i)
                x1 = self.xCentre + (self.rayon - delta) * cos(angle * i + pi)
                y0 = self.yCentre + (self.rayon - delta) * sin(angle * i)
                y1 = self.yCentre + (self.rayon - delta) * sin(angle * i + pi)
                self.getCan().create_line(x0, y0, x1, y1, width=self.traits[i]**2, fill='lightgrey')

            if self.trous[i] == 2 or self.trous[i] == 3:
                x0 = self.xCentre + (self.rayon - delta) / 1.25 * cos(angle * i) - 7
                y0 = self.yCentre + (self.rayon - delta) / 1.25 * sin(angle * i) - 7
                x1 = self.xCentre + (self.rayon - delta) / 1.25 * cos(angle * i) + 7
                y1 = self.yCentre + (self.rayon - delta) / 1.25 * sin(angle * i) + 7
                x2 = self.xCentre + (self.rayon - delta) / 1.25 * cos(angle * i + pi) - 7
                y2 = self.yCentre + (self.rayon - delta) / 1.25 * sin(angle * i + pi) - 7
                x3 = self.xCentre + (self.rayon - delta) / 1.25 * cos(angle * i + pi) + 7
                y3 = self.yCentre + (self.rayon - delta) / 1.25 * sin(angle * i + pi) + 7
                self.getCan().create_oval(x0, y0, x1, y1, fill='yellow')
                self.getCan().create_oval(x2, y2, x3, y3, fill='yellow')

        if 1 or 3 in self.traits :
                x = self.xCentre
                y = self.yCentre
                self.getCan().create_oval(x - 7, y - 7, x + 7, y + 7, fill='yellow')
        #mainloop()

    def generer(self):
        raise NotImplementedError('...')

    def getNbCotes(self):
        return self.__nbCotes

    def setNbCotes(self,nbCotes):
        self.__nbCotes=nbCotes

    def getCan(self):
        return self.__can

    def __eq__(self, other):

        nouveau_trous = self.trous
        res=()
        for i in range(len(nouveau_trous)):
            if nouveau_trous[i] == 3 or nouveau_trous[i]==1:
                nouveau_trous[i]=nouveau_trous[i]-1
                retenue=1
                res=(nouveau_trous,self.traits,retenue)
        return res==other


class PieceModele(Piece):

    def __init__(self,trous=[],traits=[]):
        self.trous=trous
        self.traits=traits
        Piece.__init__(self)

    def generer(self):
        self.trous=[randint(0,3) for i in range(self.getNbCotes()//2)]
        self.traits=[randint(0,3) for i in range(self.getNbCotes()//2)]

    def __repr__(self):
        return str(self.trous)+str(self.traits)+"   "+str(self.getNbCotes())

class PieceUsine(Piece):

    def __init__(self,trous=[],traits=[]):
        self.trous=trous
        self.traits=traits
        Piece.__init__(self)

    def generer(self):
        self.trous=[ 0 for i in range(self.getNbCotes()//2)]
        self.traits=[ 0 for i in range(self.getNbCotes()//2)]

    def trouer(self,nb):
        self.trous[0]=nb

    def tracer(self,epaisseur):
        self.traits[0]=epaisseur

    def tourner(self,angle):
        for i in range(angle):
            self.trous.insert(0,self.trous.pop())
            self.traits.insert(0,self.traits.pop())

    def __repr__(self):
        return str(self.trous) + str(self.traits) + "   " + str(self.getNbCotes())


if __name__=="__main__":
    p1=PieceModele()
    p2=PieceUsine()
    print(p1)
    p1.dessinerPieces()
    print(p1)
1

There are 1 answers

1
Vikash Singh On BEST ANSWER

Piece class takes 1 positional argument namely can.

class Piece(object):

    def __init__(self,can,x=100,y=100,r=100,nbCotes=8):
        .....

PieceModel class extends piece and is trying to do a super initialize without passing value for can (see last line in this block of code)

class PieceModele(Piece):

    def __init__(self,trous=[],traits=[]):
        self.trous=trous
        self.traits=traits
        Piece.__init__(self)