Home > Back-end >  Importing Class 'missing required positional arguments'?
Importing Class 'missing required positional arguments'?

Time:02-12

I am trying to create an instance of the class = Button. I understand that I have to initialize Button into my Menü class to use its variables. But PyCharm is telling me that 4 required positional arguments are missing. I am a beginner to Python/Pygame so if possible keep it simple:)

import pygame
import os
import sys


pygame.init()
#Button Klasse:
class Button(): #Klasse mit Namen 'Button' erstellen
  def __init__(self, x, y, image, scale): #init Methode definieren

    width = image.get_width() #Imageweite herausfinden
    height = image.get_height() #Imagehöhe herausfinden
    self.image = pygame.transform.scale(image, (int(width * scale), (int(height * scale))))
    self.rect = self.image.get_rect()  # Rechteck erstellen
    self.rect.topleft = (x, y)  # Rechteck Position definieren
    self.clicked = False #Klick-Zustand auf False gesetzt
    self.start_img = pygame.image.load('pong_test.png').convert_alpha()  # Bild für start_img ausw#hlen

#Erstellte Klasse auf Fenster platzieren
def draw(self, screen):

    action = False #Variable "action" = False setzen
    pos = pygame.mouse.get_pos() #Mausposition bestimmen und in 'pos' speichern
    if self.rect.collidepoint(pos): #Überprüfen ob Maus mit Rechteck kollidiert
        if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: #Linke Maustaste gefrückt(0)   Klick-Zustand = False dann:
            self.clicked = True #Klick-Zustand auf True setzen und:
            action = True #Action = True setzen
    if pygame.mouse.get_pressed()[0] == 0: #Andernfalls ich kein Klick dann:
        self.clicked = False #Klick-Zustand auf False setzen

    screen.blit(self.image, (self.rect.x, self.rect.y)) #Blit Imagedatei auf Rechteckposition x und y

    return action #Gibt 'action' zurück


class Highscore():

    def __init__(self):
        self.font = pygame.font.SysFont('Comic Sans MS', 30)
        self.HighscoreX = 100
        self.HighscoreY = 100
        self.Highscore = int(Highscore.readHS(self))


    def readHS(self):
        with open('Highscore.CSV', 'r') as f:
            return f.read()
    def showHS(self):
        self.high_render = self.font.render('Highscore:'   str(self.Highscore), True, (250, 0, 0))
        self.screen.blit(self.high_render, (self.HighscoreX, self.HighscoreY))




class Menü(Highscore, Button):

    def __init__(self):
        Highscore.__init__(self)
        Button.__init__(self,)

        self.fenster_height = 1000  # Fensterhöhe definieren
        self.fenster_width = 600  # Fensterbreite definieren
        self.screen = pygame.display.set_mode((self.fenster_height, self.fenster_width))
        self.run = True
        self.font = pygame.font.SysFont('Comic Sans MS', 32)
        self.screen.fill((96, 144, 189))  # Screen mit Farbe (255,255,255) füllen
        Highscore.readHS(self)
        Highscore.showHS(self)

    def runMenü(self):
        while True:  # Während "run = TRUE"
            Spiel1_button = Button(500, 300, self.start_img, 0.5)

            if Spiel1_button.draw(self.screen):


                for event in pygame.event.get():
                       # fenster schließen
                    if event.type == pygame.QUIT:

                        pygame.quit()
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        print('test')

                pygame.display.update()  # Fenster wird aktualisiert




menu = Menü()
menu.runMenü()

CodePudding user response:

The __init__ method in Button takes 5 arguments self, x, y, image, scale:

class Button(): #Klasse mit Namen 'Button' erstellen
  def __init__(self, x, y, image, scale): #init Methode definieren

Yet you only pass 1 in the Highscore init method.

Button.__init__(self,)

You'll have to either pass all arguments you defined or remove them from Button.__init__.

  • Related