How to fix the problem of getting the primary monitor infos with ScreenInfo in python
02:48 21 May 2026

I have This function that calculates the coordinates based on the screen width and height , and i get that using the screeninfo module.

def formula(self, x: int, y: int) -> Tuple[int, int]:
        screen_width: int = 0
        screen_height: int = 0
        for monitor in get_monitors():
            if monitor.isprimary:
                screen_width = monitor.width
                screen_height = monitor.height
                break
        scaling_x: int = (screen_width - 50) * (x / (self.max_x + 0.1)) + 50
        scaling_y: int = (screen_height - 50) * (y / (self.max_y + 0.1)) + 50
        return (scaling_x, scaling_y)

However, when i run it outside a virtual environement , isprimary attribute is missing which leads to a TypeError and the program crashes.

if monitor.isprimary:
       ^^^^^^^^^^^^^^^^^
AttributeError: 'Monitor' object has no attribute 'isprimary'

Is There a solution for that ?

python screen monitor