4

我刚刚编写了简单的脚本来启动应用程序,并且我正在尝试使用“SendKeys”模块将击键发送到该应用程序。有一个"Snapshot"按钮,但我无法让 Python 单击 "Snapshot"按钮,因为新窗口不在焦点上。所以我打算使用 Win32gui 模块的win32gui.FindWindowwin32gui.SetForegroundWindow功能。但它给了我error- invalid handle。我的应用名称是“DMCap”

这是Python中的代码片段:

handle = win32gui.FindWindow(0, "DMCap")  //paassing 0 as I dont know classname 
win32gui.SetForegroundWindow(handle)  //put the window in foreground

谁能帮我?这个 Python 代码正确吗?我可以这样直接发送句柄吗?

4

1 回答 1

4

如果确实有一个标题为“DMCap”的窗口,您的代码应该可以正常运行。要获取句柄和标题列表,请运行以下代码:

import win32gui
def window_enum_handler(hwnd, resultList):
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
        resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def get_app_list(handles=[]):
    mlst=[]
    win32gui.EnumWindows(window_enum_handler, handles)
    for handle in handles:
        mlst.append(handle)
    return mlst

appwindows = get_app_list()
for i in appwindows:
    print i

这将产生一个包含句柄、标题对的元组列表。

于 2013-05-27T14:06:04.213 回答