如何在Pygame中获得监视器的分辨率?

2024-04-20 09:51:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是想知道我是否有可能在Pygame中获得监视器的分辨率,然后使用这些维度来创建一个窗口,以便启动程序检测监视器的分辨率,然后自动将窗口适应全屏屏幕。

我正在使用pygame.display.set_mode((AN_INTEGER, AN_INTEGER))创建窗口。 我知道您可以使用pygame.display.Info()获取视频信息,包括监视器分辨率,但如何提取这些值,然后在pygame.display.set_mode()中使用它们???

提前谢谢你, 伊米翁


Tags: 程序infoan信息视频屏幕modedisplay
3条回答

您可以使用pygame.display.Info()

docs说:

current_h, current_w: Height and width of the current video mode, or of the desktop mode if called before the display.set_mode is called.
(current_h, current_w are available since SDL 1.2.10, and pygame 1.8.0) They are -1 on error, or if an old SDL is being used.1.8.0)

pygame.display.Info()创建具有属性current_hcurrent_w的Info对象。 在调用display.set_mode之前创建Info对象,然后使用对象中的current_hcurrent_w调用display.set_mode

示例:

infoObject = pygame.display.Info()
pygame.display.set_mode((infoObject.current_w, infoObject.current_h))

我对pygame了解不多,但这里有一种使用模块win32api的方法:

from win32api import GetSystemMetrics

width = GetSystemMetrics(0)
height = GetSystemMetrics(1)

更新:在浏览了docs之后,您似乎可以从pygame.display.Info获得它,如下所示:

width, height = pygame.display.Info().current_w, pygame.display.Info().current_h

希望这有帮助!

我不知道这是否可行,但如果您只想使用全屏窗口,请使用以下选项:

pygame.display.set_mode((0,0),pygame.FULLSCREEN)

(当然你还得import pygame)。

相关问题 更多 >