Winapi Visual Studio代码中Python脚本自动调试错误的解决

Winapi Visual Studio代码中Python脚本自动调试错误的解决,winapi,visual-studio-code,pywin32,python-3.8,Winapi,Visual Studio Code,Pywin32,Python 3.8,我有一个可以工作的Python脚本,它通过以下代码捕获主监视器工作区域的宽度和高度 # 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 = Mon

我有一个可以工作的Python脚本,它通过以下代码捕获主监视器工作区域的宽度和高度

# 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代码错误地抛出以下两个错误:


如何让Visual Studio代码识别类MonitorFromPoint和方法GetMonitorInfo实际上位于Win32 API库中?

此错误是由于Pylint引起的

事实上,
Pylint
在分析期间不运行Python代码,因此大多数非标准(不可推断)构造必须通过编写自定义代码来支持

参考:

有两种方法可以解决此问题:

  • 添加:
    #pylint:disable msg=E0611

    参考:

  • 使用最新版本的
    Pylint


此错误是由于
Pylint
引起的

事实上,
Pylint
在分析期间不运行Python代码,因此大多数非标准(不可推断)构造必须通过编写自定义代码来支持

参考:

有两种方法可以解决此问题:

  • 添加:
    #pylint:disable msg=E0611

    参考:

  • 使用最新版本的
    Pylint


作为一种解决方法,您可以使用
ctypes.windell

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)

作为一种解决方法,您可以使用
ctypes.windell

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)

谢谢你的建议,努力阳光。我相信我正在运行最新版本的pylint。阅读与您的链接相关的答案中的注释,“为什么pylint无法找到此包的模块?”似乎我可以使用GitHub存储库中的pylint或astroid版本,或者编辑pylint源代码。你知道怎么设置吗?@TomLever你试过
#pylint:disable msg=E0611
?如果您不知道如何添加,请参考。Hi Street Sun。我的直觉是不禁用消息,因为我不确定将来会禁用哪些有用的信息。谢谢你的资源。谢谢你的建议,太阳。我相信我正在运行最新版本的pylint。阅读与您的链接相关的答案中的注释,“为什么pylint无法找到此包的模块?”似乎我可以使用GitHub存储库中的pylint或astroid版本,或者编辑pylint源代码。你知道怎么设置吗?@TomLever你试过
#pylint:disable msg=E0611
?如果您不知道如何添加,请参考。Hi Street Sun。我的直觉是不禁用消息,因为我不确定将来会禁用哪些有用的信息。感谢您提供的资源。您能描述一下如何定义MonitorInfo结构吗?请查看我的更新,如果您有任何其他问题,请随时告诉我。解决方案对您有帮助吗?感谢您的登录。我正在为monitorinfo.sz设备获取字节数组b'\\.\\DISPLAY1'。我了解了如何定义第一个监视器的左上角、定义句柄、创建MonitorInfo Exa对象及其cbSize属性。GetMonitorInfo做什么?GetMonitorInfo]/]是多字节和unicode版本,请指定成员
szDevice
char
wchar\t
数组。请描述如何定义MonitorInfo结构?请参阅我的更新,如果您还有其他问题,请随时告诉我。解决方案对您有帮助吗?谢谢您的检查。我正在为monitorinfo.sz设备获取字节数组b'\\.\\DISPLAY1'。我了解了如何定义第一个监视器的左上角、定义句柄、创建MonitorInfo Exa对象及其cbSize属性。GetMonitorInfo做什么?GetMonitorInfo]/]是多字节和unicode版本,指定成员
szDevice
char
wchar\t
数组。