1

I have made a working program in which I use a pygame window. I have a picture background in the pygame window. The program registers the position where the mouse is clicked and when it is released. It outputs the distance in pixels. I want the program to output the distance in inches. Is there any way to get the physical and pixel size of any screen or monitor? Near universality within windows is a requirement of my program.

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)
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
Eric Lopez
  • 23
  • 2
  • 10

1 Answers1

4

You can get the resolution (width and height of the monitor) like so:

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

As for the physical size, it is impossible to get. You could however deduce it from the monitor's pixel density. However that information is also unattainable, unless you are willing to work with the win32 API, specifically the GetDpiForMonitor function. There are plenty of resources out there on how to utilize win32 functions in python. This thread should get you started.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53