无法捕获 window 标题 python

unable to capture window title python

以下代码片段应查找 window "Notes.txt - Notepad" 并捕获 window 的 screen-shot。

import pyautogui
import win32gui

def screenshot(window_title="Notes.txt - Notepad"):
    if window_title:
        hwnd = win32gui.FindWindow(window_title, None)
        if hwnd:
            win32gui.SetForegroundWindow(hwnd)
            x, y, x1, y1 = win32gui.GetClientRect(hwnd)
            x, y = win32gui.ClientToScreen(hwnd, (x, y))
            x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
            im = pyautogui.screenshot(region=(x, y, x1, y1))
            return im
        else:
            print('Window not found!')
    else:
        im = pyautogui.screenshot()
        return im


im = screenshot('Calculator')
if im:
    im.show()

这里的问题是无论我传递什么 window_title 它总是 return:

Window not found!

当我 print(hwnd) 它评估为 0

文件标题:

问题出在这里 - hwnd = win32gui.FindWindow(window_title, None)

将其替换为 win32gui.FindWindowEx(None, None, None, window_title),它应该可以工作。

Docs

编辑:

win32gui.FindWindow(None, window_title) 应该也可以。