在 Python 中获取物理屏幕尺寸
Getting Physical Screen Size in Python
我制作了一个工作程序,其中使用了 pygame window。我在pygamewindow有一张图片背景。该程序记录了鼠标被点击的位置以及何时被释放。它以像素为单位输出距离。我希望程序以英寸为单位输出距离。有没有办法获得任何屏幕或监视器的物理和像素大小? windows 内的近乎普遍性是我的程序的要求。
import pygame, sys, math
LEFT = 1
pos1x = 0
pos2x = 0
pos1y = 0
pos2y = 0
running = True
screen = pygame.display.set_mode((1200, 1000))
back_image = pygame.image.load("Monte_Alban.jpg")
screen.blit(back_image, [0,0])
while running:
pygame.display.flip()
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
pos1x,pos1y = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
pos2x,pos2y = pygame.mouse.get_pos()
distx = abs(pos2x-pos1x)
disty = abs(pos2y-pos1y)
print ("Horizontal distane equals: %d" % distx)
print ("Vertical distance equals: %d" % disty)
您可以像这样获取分辨率(显示器的宽度和高度):
info = pygame.display.Info()
width = info.current_w
height = info.current_h
至于实物尺寸,是拿不到的。但是,您可以从显示器的像素密度中推断出来。然而,该信息也是无法获得的,除非你愿意使用 win32 API,特别是 GetDpiForMonitor function. There are plenty of resources out there on how to utilize win32 functions in python. This 线程应该让你开始。
我制作了一个工作程序,其中使用了 pygame window。我在pygamewindow有一张图片背景。该程序记录了鼠标被点击的位置以及何时被释放。它以像素为单位输出距离。我希望程序以英寸为单位输出距离。有没有办法获得任何屏幕或监视器的物理和像素大小? windows 内的近乎普遍性是我的程序的要求。
import pygame, sys, math
LEFT = 1
pos1x = 0
pos2x = 0
pos1y = 0
pos2y = 0
running = True
screen = pygame.display.set_mode((1200, 1000))
back_image = pygame.image.load("Monte_Alban.jpg")
screen.blit(back_image, [0,0])
while running:
pygame.display.flip()
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
pos1x,pos1y = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
pos2x,pos2y = pygame.mouse.get_pos()
distx = abs(pos2x-pos1x)
disty = abs(pos2y-pos1y)
print ("Horizontal distane equals: %d" % distx)
print ("Vertical distance equals: %d" % disty)
您可以像这样获取分辨率(显示器的宽度和高度):
info = pygame.display.Info()
width = info.current_w
height = info.current_h
至于实物尺寸,是拿不到的。但是,您可以从显示器的像素密度中推断出来。然而,该信息也是无法获得的,除非你愿意使用 win32 API,特别是 GetDpiForMonitor function. There are plenty of resources out there on how to utilize win32 functions in python. This 线程应该让你开始。