如何获得任何计算机的屏幕尺寸

How to get the screen size of any computer

所以,我浏览了整个互联网,看到了一些说明

window_width, window_height = pygame.display.get_surface().get_size()

是可能的,但是当我这样做时,我收到一条错误消息 (window_width = pygame.display.get_surface().get_size() AttributeError: 'NoneType' 对象没有属性 'get_size') 我已经看到它在这里和其他网站是可能的,为什么它对我不起作用?

import pygame 
import random
window_width, window_height = pygame.display.get_surface().get_size()
black = (0,0,0)
close_program = False
fullscreen = False
white = (255,255,255)
random_position_x = random.randint(0, window_width)
random_position_y = random.randint(0, window_height)

您可以使用 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 的信息对象。在调用 display.set_mode 之前创建信息对象,然后使用对象中的 current_hcurrent_w 调用 display.set_mode。

示例 1:

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

示例 2:

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