Tim Trudeau
Tim Trudeau

Reputation: 51

Pygame window fails to open from inside python script

I am trying to open a pygame surface on a Windows 10 PC. Following, are the steps needed to open a 800 by 600 window. The surface opens and displays correctly when invoked from a python shell as follows:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
>>> import pygame
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
(6, 0)
>>> size = (800, 600)
>>> pygame.display.set_mode(size)
<Surface(800x600x32 SW)>
>>> pygame.display.get_surface()
<Surface(800x600x32 SW)> 

When I do the same operation within a python script, a get a trace back error:

pygame.error: No available video device**

pygame.init()
self.window = pygame.display.set_mode((width, height))
self.screen = pygame.display.get_surface()
self.background = pygame.surface.Surface(self.screen.get_size()).convert()
self.background.fill((0, 0, 0))
pygame.mouse.set_visible(True)

File "C:\Users\timtru\Documents\PycharmProjects\core.py", line 25, in __init__ super(Pypboy, self).__init__(*args, **kwargs) File "C:\Users\timtru\Documents\PycharmProjects\core.py", line 21, in __init__ self.window = pygame.display.set_mode((width, height)) **pygame.error: No available video device**

Why can I get the window in the shell, but get "No available video device" when the same steps are run in a script?

Upvotes: 2

Views: 327

Answers (1)

Resistnz
Resistnz

Reputation: 72

At the start of the program, after you call pygame.init(), you need to call

pygame.display.init()

You need to initialise the display before you can use it. From the docs,

The display module cannot do anything until it is initialized.

Upvotes: 1

Related Questions