1

I'm trying to get my screen size using python. I keep getting the incorrect value because my code is taking into account the scale factor. For example: My screen resolution is set to: 2736 x 1824. My scale factor is 200% so when I execute my code I get 1368 x 912.

import win32api
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
print('Width:', width)
print('Height:', height)

Is there any way I can get the resolution as shown in my windows settings without the scale factor? I want to be able to read 2736 x 1824.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

2 Answers2

4
import ctypes

scaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100

You can get the scale factor via shcore using ctypes. Then from this you can calculate the real resolution.

ie: scaleFactor 1.25 is 125%

GO.exe
  • 646
  • 7
  • 13
1

Your application is not DPI aware. Windows has to lie to the application about the dimensions and magnify the GUI to fit the scale.

A quick fix:

import ctypes
ctypes.windll.user32.SetProcessDPIAware()

For an executable it is recommended to set DPI awareness in the manifest file.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    Do you know how to set it in the manifest file created by pyinstaller? Or is it an post-creation thing anyway? Thanks in advance. – Nummer_42O Jan 19 '20 at 17:43