Python 如何在 Pygame 中获得显示器的分辨率?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19954469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:03:42  来源:igfitidea点击:

How to get the resolution of a monitor in Pygame?

pythonwindowpygameresolutiondimensions

提问by Ilmiont

I'm just wondering if it is possible for me to get the resolution of a monitor in Pygame and then use these dimensions to create a window so that launching the program detects the monitor resolution and then automatically fits the window to the screen in fullscreen.

我只是想知道是否有可能在 Pygame 中获得显示器的分辨率,然后使用这些尺寸创建一个窗口,以便启动程序检测显示器分辨率,然后自动使窗口适合全屏屏幕。

I am currently using pygame.display.set_mode((AN_INTEGER, AN_INTEGER))to create the window. I am aware that you can get video info including the monitor resolution using pygame.display.Info()but how can I extract these values and then use them in pygame.display.set_mode()???

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

Thanks in advance, Ilmiont

提前致谢, Ilmiont

采纳答案by Mailerdaimon

You can use pygame.display.Info():

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

The docssay:

文件说:

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)

current_h, current_w:当前视频模式的高度和宽度,如果在调用 display.set_mode 之前调用,则为桌面模式的高度和宽度。
(current_h、current_w 自 SDL 1.2.10 和 pygame 1.8.0 起可用)它们在错误时为 -1,或者如果正在使用旧的 SDL。1.8.0)

pygame.display.Info()creates an Info Object with the attributes current_hand current_w. Create the Info Object before you call display.set_modeand then call display.set_modewith current_hand current_wfrom the object.

pygame.display.Info()创建一个具有属性current_h和的信息对象current_w。在调用之前创建 Info 对象display.set_mode,然后display.set_mode使用该对象current_hcurrent_w从该对象进行调用。

Example:

例子:

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

回答by aIKid

I don't know much about pygame, but here is a way using the module win32api:

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

from win32api import GetSystemMetrics

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

Update: After taking a glance at the docs, seems like you can get it from pygame.display.Info, like this:

更新:看了一眼文档后,似乎您可以从 获取它pygame.display.Info,如下所示:

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

Hope this helps!

希望这可以帮助!

回答by Hack5

I don't know if this will work, but if you just want a fullscreen window use the following:

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

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

(of course you still have to import pygame).

(当然你仍然必须import pygame)。