Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.

Answer from Ignacio Vazquez-Abrams on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › object has no attribute error, but it actually does..
r/learnpython on Reddit: Object has no attribute error, but it actually does..
September 17, 2022 -

hello, i am working on a game using pygame and i am going for an OOP approach, the files are pretty big, i will post only a portion of the code; also i am using pycharm so i know for a fact my identation, typing are alright.. plus i have been coding in python for a while now but i just can't grasp onto this issue :(, so far in the game i am just in the main menu phase and whenever i try to run my code i get an error that my game object (which has a reference to another class including the main menu game loop) has no attribute ''screenWidth'' but it actually does have a variable for my screen.. i just use it in the main menu to set another midWidth variable half of that variable as value
here is the code:

main.py

from game import Game

myGame = Game()

while myGame.running:
    myGame.currentMenu.drawMainMenu()
    myGame.gameLoop()

game.py

import pygame
from menu import MainMenu


class Game:
    def __init__(self):
        pygame.init()

        self.running, self.playing = True, False
        self.upKey, self.downKey, self.selectKey, self.backKey = False, False, False, False

        self.currentMenu = MainMenu(self)

        self.screenWidth = 1280
        self.screenHeight = 720
        self.gameWindow = pygame.display.set_mode((self.screenWidth, self.screenHeight))
        pygame.display.set_caption("Game Prototype")

        self.backgroundImage = pygame.image.load('Assets/Images/bg_greek.jpg')
        self.backgroundImage = pygame.transform.scale(self.backgroundImage, (1920, 1080))

    def gameLoop(self):
        while self.playing:
            self.checkEvents()

            if self.selectKey:
                self.playing = False

            self.gameWindow.blit(self.backgroundImage, (-600, -300))

            pygame.display.flip()
            self.resetKeys()

menu.py

import pygame


class Menu:
    def __init__(self, game):
        self.gameClass = game
        self.midWidth = self.gameClass.screenWidth / 2
        self.midHeight = self.gameClass.screenHeight / 2
        self.showMenu = True
        self.selectionX, self.selectionY = 0, 0

    def drawSelection(self):
        selectSurface = pygame.Surface((260, 100), pygame.SRCALPHA)
        selectSurface.fill((50, 50, 50, 175))
        selectRect = selectSurface.get_rect()
        selectRect.center = (self.selectionX, self.selectionY)
        self.gameClass.gameWindow.blit(selectSurface, selectRect)


class MainMenu(Menu):
    def __init__(self, game):
        Menu.__init__(self, game)
        self.hoveredState = "Start"
        self.startX, self.startY = self.midWidth, self.midHeight
        self.optionsX, self.optionsY = self.midWidth, self.midHeight + 100
        self.creditsX, self.creditsY = self.midWidth, self.midHeight + 200
        self.quitX, self.quitY = self.midWidth, self.midHeight + 300
        self.selectionX, self.selectionY = self.startX, self.startY

    def drawMainMenu(self):
        self.showMenu = True
        while self.showMenu:
            self.gameClass.checkEvents()
            self.checkInput()

            self.gameClass.gameWindow.blit(self.gameClass.backgroundImage, (-600, -300))
            self.drawSelection()
            self.gameClass.drawText("Play", 72, self.startX, self.startY)
            self.gameClass.drawText("Options", 72, self.optionsX, self.optionsY)
            self.gameClass.drawText("Credits", 72, self.creditsX, self.creditsY)
            self.gameClass.drawText("Quit", 72, self.quitX, self.quitY)

            pygame.display.flip()
            self.gameClass.resetKeys()

it is not complete though

Discussions

Hey Guys, I am a begineer and I need some help. I got this attribute error. "AttributeError: 'Employee' object has no attribute 'getdetails' ". Can someone help me?
Hey Guys, I am a begineer and I need some help. I got this attribute error. "AttributeError: 'Employee' object has no attribute 'getdetails' ". Can someone help me · Any function that is trying to access self. needs to be a method. To be a method, it needs to accept self as a parameter, ... More on discuss.python.org
🌐 discuss.python.org
0
0
November 21, 2022
Don't know how to fix AttributeError: 'function' object has no attribute 'pk' when trying to create new users from registration form
Hi, I’m still very new to Python and Django. I am trying to create a working registration page and i get this error when submitting the form. I can’t find this error when looking on stack overflow or the forum and don’t know how to fix this issue. I copied the views and models code here. More on forum.djangoproject.com
🌐 forum.djangoproject.com
0
0
September 2, 2023
Error in py_repr(x) : AttributeError: ' ' object has no attribute ' '
There was an error while loading. Please reload this page · The following command lines More on github.com
🌐 github.com
1
July 11, 2022
Attribute error. How to fix it?
import turtle t = turtle.pen() t.forward(60) Traceback (most recent call last): File “ ”, line 1, in t.forward(60) AttributeError: ‘dict’ object has no attribute ‘forward’ I reinstalled python, but it didn’t work. Please help More on discuss.python.org
🌐 discuss.python.org
0
0
June 30, 2020
People also ask

How to solve the error in Python, "Class object has no attribute_name"?
To solve this error, we should check if the attribute is declared in the class or not. Further, if the attribute name has typos and case differences or if is declared privately in the class, it will lead to error at runtime.
🌐
askpython.com
askpython.com › home › how to fix the ‘class’ object has no ‘attribute_name’ error in python
How to Fix the 'Class' object has no 'attribute_name' Error in ...
Which Python method can prevent Attribute Errors?
Most of the time, an Attribute Error occurs because either the attribute is missing, is out of scope (private) or the name contains typos and case differences. Thus to check for such cases Python provides us a method named "hasattr()'. The method is a boolean function and returns true and false, after the check.
🌐
askpython.com
askpython.com › home › how to fix the ‘class’ object has no ‘attribute_name’ error in python
How to Fix the 'Class' object has no 'attribute_name' Error in ...
🌐
Rollbar
rollbar.com › home › how to fix attributeerror in python
How to Fix AttributeError in Python | Rollbar
October 17, 2022 - The Python help() function can be used to find out all attributes and methods related to the object. To resolve the AttributeError, a try-except block can be used. The lines of code that can throw the AttributeError should be placed in the try block, and the except block can catch and handle the error. Using the above approach, the previous example can be updated to handle the error: i = 1 try: i.append(2) except AttributeError: print('No such attribute')>
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-attributeerror-object-has-no-attribute
How to fix AttributeError: object has no attribute - GeeksforGeeks
July 23, 2025 - Let us start! The "AttributeError: Object has no attribute" error is a common issue in Python. It occurs when we try to access an attribute of an object that doesn't exist for that object.
🌐
JanBask Training
janbasktraining.com › community › python-python › why-am-i-getting-attributeerror-object-has-no-attribute
Why am I getting AttributeError: Object has no attribute | JanBask Training Community
April 17, 2021 - The "AttributeError: Object has no attribute" error occurs in Python when you try to access or call an attribute or method that does not exist on an object.
🌐
GeeksforGeeks
geeksforgeeks.org › python-attributeerror
Python: AttributeError - GeeksforGeeks
January 3, 2023 - --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-9-c26cd169473f> in <module> ----> 1 d.getkeys() AttributeError: 'dict_parsing' object has no attribute 'getkeys' Errors and exceptions in Python can be handled using exception handling i.e.
Find elsewhere
🌐
AskPython
askpython.com › home › how to fix the ‘class’ object has no ‘attribute_name’ error in python
How to Fix the 'Class' object has no 'attribute_name' Error in Python - AskPython
April 10, 2025 - The “AttributeError: ‘Class’ ... Python occurs when an object tries to access an attribute not defined on its class, often due to typos, missing attributes, scope issues with private attributes, or inheritance problems...
🌐
sebhastian
sebhastian.com › object-has-no-attribute-python-class
How to fix AttributeError: object has no attribute in Python class | sebhastian
February 17, 2023 - With the attribute defined inside the class, you resolved this error. Suppose you have a class with the following indentations in Python: class Human: def __init__(self, name): self.name = name def walk(): print("Walking") Next, you created a Human object and call the walk() method as follows: ... Traceback (most recent call last): File "main.py", line 9, in <module> person.walk() AttributeError: 'Human' object has no attribute 'walk'
🌐
LabEx
labex.io › tutorials › python-how-to-resolve-attributeerror-in-python-415870
How to resolve AttributeError in Python | LabEx
person = None if person is not ... AttributeError due to an incorrect object type, you need to ensure that you're accessing the correct attributes for the given object type....
🌐
Delft Stack
delftstack.com › home › howto › python › python object has no attribute
How to Fix Object Has No Attribute Error in Python | Delft Stack
February 2, 2024 - We can also update our object to the type that supports the required attribute. However, this is not a good method and may lead to other unwanted errors. We can also use the hasattr() function. This function returns True if an attribute belongs to the given object.
🌐
Python.org
discuss.python.org › python help
Hey Guys, I am a begineer and I need some help. I got this attribute error. "AttributeError: 'Employee' object has no attribute 'getdetails' ". Can someone help me? - Python Help - Discussions on Python.org
November 21, 2022 - Hey Guys, I am a begineer and I need some help. I got this attribute error. "AttributeError: 'Employee' object has no attribute 'getdetails' ". Can someone help me · Any function that is trying to access self.<thing> needs to be a method. To be a method, it needs to accept self as a parameter, ...
🌐
YouTube
youtube.com › teclado
Python AttributeError — What is it and how do you fix it? - YouTube
AttributeError: '***' object has no attribute '***' What is an AttributeError in Python? What can you do to fix it? When does it happen? All these questions ...
Published   July 27, 2018
Views   44K
🌐
CodeWithHarry
codewithharry.com › blogpost › attribute-error-in-python
[Solved] Python AttributeError: object has no attribute 'X' | Blog | CodeWithHarry
April 5, 2025 - The error emerges because the __init__ method of the Car class does not initialize the speed attribute at all. class BankAccount: interest_rate = 0.05 my_account = BankAccount() print(my_account.interest_rate) # Attribute Error: 'BankAccount' object has no attribute 'interest_rate'
🌐
Career Karma
careerkarma.com › blog › python › python attributeerror: a how-to guide
Python AttributeError: A How-To Guide | Career Karma
December 1, 2023 - AttributeError: ‘module’ object has no attribute ‘urlopen’ · Attribute errors in Python are raised when an invalid attribute is referenced. To solve these errors, first check that the attribute you are calling exists.
🌐
Django Forum
forum.djangoproject.com › using django › forms & apis
Don't know how to fix AttributeError: 'function' object has no attribute 'pk' when trying to create new users from registration form - Forms & APIs - Django Forum
September 2, 2023 - Hi, I’m still very new to Python and Django. I am trying to create a working registration page and i get this error when submitting the form. I can’t find this error when looking on stack overflow or the forum and don’t know how to fix this issue. I copied the views and models code here.
🌐
Net Informations
net-informations.com › python › err › attr.htm
Module object has no attribute error: Python
In other words, the attributes ... access or call an attribute that a particular object type doesn't possess. It's simply because there is no attribute with the name you called, for that Object....
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 171777 › error-object-has-no-attribute-xxx
python - Error: object has no attribute 'xxx' [SOLVED] | DaniWeb
I would be thankfull. ... The AttributeError here is a classic side effect of mixing tabs and spaces: a function that visually looks like a class method may actually be indented inside another method.
🌐
GitHub
github.com › rstudio › reticulate › issues › 1235
Error in py_repr(x) : AttributeError: ' ' object has no attribute ' ' · Issue #1235 · rstudio/reticulate
July 11, 2022 - Error in py_repr(x) : AttributeError: 'KMeans' object has no attribute 'k' Is this a bug? Or I'm doing something wrong. No one assigned · No labels · No labels · No type · No projects · No milestone · None yet · No branches or pull requests ·
Author   nipnipj
🌐
Quora
quora.com › What-does-it-mean-when-Python-says-AttributeError-module-object-has-no-attribute
What does it mean when Python says 'AttributeError: ‘module’ object has no attribute”? - Quora
December 19, 2022 - Answer: An `AttributeError` in ... "AttributeError: 'module' object has no attribute 'something'" typically indicates that you are trying to access an attribute (in this case,......
🌐
Python.org
discuss.python.org › python help
Attribute error. How to fix it? - Python Help - Discussions on Python.org
June 30, 2020 - import turtle t = turtle.pen() t.forward(60) Traceback (most recent call last): File “<pyshell#2>”, line 1, in t.forward(60) AttributeError: ‘dict’ object has no attribute ‘forward’ I reinstalled python, but it…