【问题标题】:Pygame window fails to open from inside python scriptPygame 窗口无法从 python 脚本内部打开
【发布时间】:2019-09-22 15:20:47
【问题描述】:

我正在尝试在 Windows 10 PC 上打开一个 pygame 界面。以下是打开 800 x 600 窗口所需的步骤。当从 python shell 调用时,表面会打开并正确显示,如下所示:

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)> 

当我在 python 脚本中执行相同的操作时,出现回溯错误:

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**

为什么我可以在 shell 中获取窗口,但在脚本中运行相同的步骤时却得到“没有可用的视频设备”?

【问题讨论】:

    标签: python pygame-surface


    【解决方案1】:

    程序开始时,调用pygame.init()后,需要调用

    pygame.display.init()
    

    您需要先初始化显示器,然后才能使用它。来自docs

    显示模块在初始化之前不能做任何事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多