I am attempting to make my pygame application more modular so that different functionalities are split up into different classes and modules. I am having some trouble getting pygame to allow me to draw or load images in secondary classes when the display has been set and pygame.init() has been done in my main class.

I have typically used C# and XNA to accomplish this sort of behavior, but this time I need to use python.

How do I init Pygame in class1, then create an instance of class2 which loads and converts() images.

I have tried pygame.init() in class 2 but then it tells me no display mode has been set, when it has been set in class1. I am under the impression I do not want to create multiple Pygame displays as that gets problematic.

I am probably missing something pythonic and simple but I am not sure what. How do I create a Display class, init Python and then have other modules do my work like loading images, fonts etc.?

Here is the simplest version of what I am doing:

    def __init__(self):
        self.screen = pygame.display.set_mode((600,400))
        self.imageLoader = class2()

    def __init__(self):
        self.images = ['list of images']

    def load_images():
        self.images = os.listdir('./images/') #get all images in the images directory
        for img in self.images:
            #read all images in the directory and load them into pygame
            new_img = pygame.image.load(os.path.join('images', img)).convert()
            scale_img = pygame.transform.scale(new_img, (pygame.display.Info().current_w, pygame.display.Info().current_h))

if __name__ == "__main__":
    c1 = class1()

Of course when it tries to load an convert the images it tells me Pygame has not been initialized, so I throw in a pygame.init() in class2 (I have heard it is safe to init multiple times) and then the error goes to:

pygame.error: No video mode has been set


1 Answer 1


This is what you need to do, in the following order:

  1. pygame.init() - this needs to happen before any other use of pygame
  2. pygame.display.set_mode() - this needs to be done before any other use of pygame.display
  3. Load and scale your images. Note that pygame.image.load() doesn't need pygame.display.set_mode(), but you're also using pygame.display.Info() to scale your images, hence the order.

You may find this cheatsheet useful: http://inventwithpython.com/pygamecheatsheet.png

When you get an error in Python, you also get the exact line of code that caused that error. That should help tremendously in troubleshooting.

  • \$\begingroup\$ It was all about the order I was doing it weird and trying to load and convert images before I had set the display. thanks! \$\endgroup\$
    – sec_goat
    Jun 12, 2014 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .