3

In Settings > Display it says that my screen resolution is set to 1920x1080 enter image description here. But I've tried to get it with 3 different methods in python:

1- With Tkinter:

from tkinter import *
root = Tk()
Width = root.winfo_screenwidth()
Height= root.winfo_screenheight()

2- With win32api:

from win32api import GetSystemMetrics
Width = GetSystemMetrics(0)
Height = GetSystemMetrics(1)

3- With ctypes:

import ctypes
Width = ctypes.windll.user32.GetSystemMetrics(0)
Height = ctypes.windll.user32.GetSystemMetrics(1)

All of these methods keep returning Width = 1536 and Height = 864 and not the resolution that it says in my display settings.

How could I get the same resolution as displayed in the Display Settings (1920x1080)?

The_Fishy
  • 143
  • 10

1 Answers1

1

This problem is caused by the scaling setting that is set to 125%.

So I found 2 solutions:

The First one answered by @spacether in @rectangletangle's question:

import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
Width = user32.GetSystemMetrics(0)
Height = user32.GetSystemMetrics(1)

The Second one:

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
  • 2nd link is broken. – Shmack May 01 '23 at 20:31
  • @Shmack It seems that the page isn’t available anymore, but I still got the code that we need in my answer, so no need for the rest of the website – The_Fishy May 03 '23 at 20:17
  • 1
    Okay, that's fine. I didn't know if you wanted to hunt down the website that you were referencing or not. Just wanted to let you know. – Shmack May 03 '23 at 20:19