win32gui.GetPixel() not working: pywintypes.error: (0, 'GetPixel', 'No error message is available')

win32gui.GetPixel() not working: pywintypes.error: (0, 'GetPixel', 'No error message is available')

在我自己的 pysimplegui 程序和其他程序中,每次使用 GetPixel 时,我都会收到错误消息:pywintypes.error: (0, 'GetPixel', 'No error message is available').

while True:                             # The Event Loop
event, values = window.read()
hwnd = win32gui.FindWindow(None, 'Window that stays open')
rgbint2rgbtuple(win32gui.GetPixel(hwnd, 100, 100))

print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
    break

唯一没有崩溃的是 google chrome,但它给出了错误的 RGB 值。有什么想法吗?

您可以使用以下方式获取像素值,

import win32ui

w = win32ui.FindWindow(None, 'Window that stays open')
dc = w.GetWindowDC()
color = dc.GetPixel(100, 100)

如果 window 'Window that stays open' 未找到或标题错误,应检查大小写。通过以下代码确认您的 window 的标题。还要确认 window.

中的 point(x, y)
import win32gui

def enumWindowFunc(hwnd, windowList):
    text = win32gui.GetWindowText(hwnd)
    className = win32gui.GetClassName(hwnd)
    if text and className != "IME":
        print(repr(text), repr(className))

win32gui.EnumWindows(enumWindowFunc, [])