-2

So,This was my code,i was coding normally by watching a tutorial but suddenly when i used the fill attribution, an error popped up saying the following :

line 15, in display.fill((25, 25, 29)) AttributeError: 'NoneType' object has no attribute 'fill'

And under is the code,that i wrote,if anybody willingly helped me then,i would be very happy!

Under Bellow is my code

import pygame

pygame.init()

pygame.display.set_mode((800, 600))

display = pygame.display.set_caption("Space Invaders!")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

display.fill((25, 25, 29))
pygame.display.update()
  • The [docs](https://www.pygame.org/docs/ref/display.html#pygame.display.set_caption) mention that `pygame.display.set_caption` returns `None`. – Nikhil Kumar May 12 '21 at 18:04
  • @NikhilKumar yeah it does,but with this code,i had run it,and it was not a problem but today its saying fill attribution is not found – Mushfiqur Rahman May 12 '21 at 18:07
  • also yeah i know,what was that to you by writting that,i just wanted to know why my code is not working and how to fix it :( – Mushfiqur Rahman May 12 '21 at 18:16

2 Answers2

3

While I haven't got pygame so I can't test the code, I strongly suspect your issue is related to these three lines and how they relate to each other:

pygame.display.set_mode((800, 600))

display = pygame.display.set_caption("Space Invaders!")

display.fill((25, 25, 29))

You have set the display mode and now you want to fill it. However, you haven't actually assigned the output of display.set_mode() to display, you've assigned the output of display.set_caption() - which, as someone else has already commented, is nothing as display.set_caption() doesn't return a value.

So, when you try to use display, it doesn't contain anything.

Consider trying the following code instead (though I don't know if the order is important):

display = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Space Invaders!")
EvillerBob
  • 164
  • 7
1

I suspect that pygame failed to initialize. This propogated to:

display = pygame.display.set_caption("Space Invaders!")

Returning 'NoneType' object which finally fails when you run:

display.fill((25, 25, 29))

use a breakpoint at "display =..." to see the return value.

after looking further....it's syntax/formatting related. Here are my corrections to get it going:

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
display = pygame.display.set_caption("Space Invaders!")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        screen.fill((25, 25, 29))
        pygame.display.update()
Shawn Ramirez
  • 796
  • 1
  • 5
  • 10
  • Can you tell me why its a error and what to do fix it?and i didnt understood you – Mushfiqur Rahman May 12 '21 at 18:05
  • Yes, if you close the display (by pressing the close window button) then the display is no longer there and object becomes none. The code is fairly basic in that it does not perform any error handling when updating the window. – Shawn Ramirez May 12 '21 at 18:10
  • So to change the background color,what should i do if i cant use the fill command?@ShawnRaimrez – Mushfiqur Rahman May 12 '21 at 18:12
  • Also @ShawnRamirez yes i understood the fact that i close the display and its no longer there,but i cant even run it its saying attribution nontype object has no attribute fill and i even putted it on the while loop to see if it works,but no it doesnot :(,Help me! – Mushfiqur Rahman May 12 '21 at 18:15
  • I added more to my original response; I'm still new myself so thanks for the opportunity to try and figure that problem out! :-) – Shawn Ramirez May 12 '21 at 18:27
  • man you are right too! – Mushfiqur Rahman May 12 '21 at 18:28