Wispaman8
Wispaman8

Reputation: 61

I keep receiving an attribute error in Pygame when attempting to create a window

I have only today started using pygame, and am just getting familiar with how to use it. I want to start out by simply creating a blank window. I have attempted using the code below:

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

while not done:
    for event in pygame.display.event.get():
        if event.type == pygame.QUIT():
            done = True
    pygame.display.flip()

However, the window appears for only a second before disappearing, followed by the error message:

line 8, in for event in pygame.display.event.get(): AttributeError: module 'pygame.display' has no attribute 'event'

Can anyone help? Thanks

Upvotes: 1

Views: 36

Answers (2)

RatherBKnitting
RatherBKnitting

Reputation: 421

The documentation shows event as an attribute of the pygame module. I think you should change the line to pygame.event.get().

This makes sense given the error.

Upvotes: 1

Kingsley
Kingsley

Reputation: 14906

The event queue is accessed via the function pygame.event.get(). You are trying to use the pygame display. Also QUIT is not a function.

Please try:

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

while not done:
    for event in pygame.event.get():   # <-- HERE
        if event.type == pygame.QUIT:  # <-- AND HERE
            done = True
    pygame.display.flip()

Upvotes: 2

Related Questions