1

I am trying to resize my window app according to the resolution. My computer resolution as we can see in the screenshot is 3840 x 2160 with a 300% scaling. With this code:

from win32api import GetSystemMetrics
import win32con

print("Width =", GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN))
print("Height =", GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN))

I get :

Width = 1280
Height = 720

Because of the scaling

My screen settings

wjandrea
  • 28,235
  • 9
  • 60
  • 81
YuSinn
  • 21
  • 3
  • Does this help? https://stackoverflow.com/a/66248631/3991125 – albert Apr 27 '21 at 14:24
  • Getting the same values – YuSinn Apr 27 '21 at 14:34
  • as i know scaling has nothing to do with resolution – Bakkar Bakkar Apr 28 '21 at 08:36
  • The problem is that if I use the scaling 300% when I try to get my resolution with the code I have shown I obtain 3840/3= 1280 x 2160/3 = 720. For a resolution of 1920 x 1080 with a scaling of 150%, I am getting again 1280 x 720 instead of 1920 x 1080. @BakkarBakkar – YuSinn Apr 28 '21 at 08:53
  • The win32 api/con simply takes the size of the "screen" whose area equates to yours under your chosen scaling. If you want to adjust for a given scaling, just multiply the api/con result by the appropriate factor (3 in this case). – kendfss Apr 28 '21 at 12:42

2 Answers2

0

To get the actual resolution as if your scaling is set to 100% you'll have to use this method:

import win32con, win32gui, win32print
def get_dpi():
  hDC = win32gui.GetDC(0)
  HORZRES = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
  VERTRES = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)
  return HORZRES,VERTRES
The_Fishy
  • 143
  • 10
-1

EDIT: I came across the actual value here: device pixel ratio

If your scale is 150% your pixel ratio will be 1.5.

from PySide6 import QtGui

screen = QtGui.QGuiApplication.primaryScreen()
scaled_pixel_ratio = screen.devicePixelRatio()
Mike Bourbeau
  • 481
  • 11
  • 29