Tom Lever
Tom Lever

Reputation: 435

Resolving Error in Automatic Debugging of Python Script in Visual Studio Code

I have a working Python script that captures the width and height of the work area of my primary monitor by the following code.

# First, install library win32api by executing as administrator
# the command "pip install pywin32" in PowerShell. Then,

from win32api import MonitorFromPoint, GetMonitorInfo
handle_for_primary_monitor = MonitorFromPoint((0,0))
monitor_info = GetMonitorInfo(handle_for_primary_monitor)
work_area_info = monitor_info.get("Work")
width_of_work_area = work_area_info[2]
height_of_work_area = work_area_info[3]

Visual Studio Code erroneously throws the following two errors:

Error_in_Automatic_Debugging_of_Python_Script_in_VSCode

How do I get Visual Studio Code to recognize that class MonitorFromPoint and method GetMonitorInfo actually are in the win32api library?

Upvotes: 0

Views: 284

Answers (2)

Drake Wu
Drake Wu

Reputation: 7170

As a workaround, you can use the ctypes.windll:

import ctypes
from ctypes.wintypes import tagPOINT
from ctypes import *
class RECT(Structure):
    _fields_ = [
        ("left", c_long),
        ("top", c_long),
        ("right", c_long),
        ("bottom", c_long),
    ]
class MONITORINFOEXA(Structure):
    _fields_ = [
        ("cbSize", c_ulong),
        ("rcMonitor", RECT),
        ("rcWork", RECT),
        ("dwFlags", c_ulong),
        ("szDevice", c_char*32),
    ]
class MONITORINFOEXW(Structure):
    _fields_ = [
        ("cbSize", c_ulong),
        ("rcMonitor", RECT),
        ("rcWork", RECT),
        ("dwFlags", c_ulong),
        ("szDevice", c_wchar*32),
    ]
point = tagPOINT(0,0)
handle_for_primary_monitor = ctypes.windll.user32.MonitorFromPoint(point,0)
print(handle_for_primary_monitor)
monitorinfo = MONITORINFOEXW()
#monitorinfo.cbSize = 72 #sizeof(MONITORINFOEXA) = 72 ;sizeof(MONITORINFOEXW) = 104
monitorinfo.cbSize = 104
monitorinfo.dwFlags = 0x01 #MONITORINFOF_PRIMARY
#ctypes.windll.user32.GetMonitorInfoW(handle_for_primary_monitor,byref(monitorinfo))
ctypes.windll.user32.GetMonitorInfoW(handle_for_primary_monitor,byref(monitorinfo))
Monitor_width =  monitorinfo.rcMonitor.right - monitorinfo.rcMonitor.left
Monitor_height =  monitorinfo.rcMonitor.bottom - monitorinfo.rcMonitor.top
Work_width = monitorinfo.rcWork.right - monitorinfo.rcWork.left
Work_height = monitorinfo.rcWork.bottom - monitorinfo.rcWork.top
print(monitorinfo.szDevice)
print(Monitor_width)
print(Monitor_height)
print(Work_width)
print(Work_height)

Upvotes: 1

Strive Sun
Strive Sun

Reputation: 6289

This error is due to Pylint.

In fact, Pylint does not run Python code during analysis, so most of non-standard (non-inferable) constructs have to be supported by writing custom code.

Refer: Why is pylint unable to find this package's module(s)?

There are two ways to solve this problem:

Upvotes: 1

Related Questions