Python:win32gui.SetForegroundWindow [英] Python: win32gui.SetForegroundWindow

查看:2086
本文介绍了Python:win32gui.SetForegroundWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚编写了简单的脚本来启动应用程序,并且我试图使用"SendKeys"模块将击键发送到此应用程序.有一个"Snapshot"按钮,但是我无法让Python单击"Snapshot"按钮,因为新窗口没有对准焦点.因此,我计划使用Win32gui模块的win32gui.FindWindowwin32gui.SetForegroundWindow功能.但这给了我error- invalid handle.我的应用名称是"DMCap"

I have just written simple script to launch an applciation and I am trying to use "SendKeys" module to send keystrokes to this application. There is one "Snapshot" button, but I cant get Python to click "Snapshot" button, as the new window is not in focus. So I am planning to use Win32gui module's win32gui.FindWindow and win32gui.SetForegroundWindow functionality. But it gives me error- invalid handle. My app name is "DMCap"

这是Python中的代码段:

Here is code snippet in Python:

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

有人可以帮助我吗?这个Python代码正确吗?我可以像这样直接发送句柄吗?

Can anyone help me? Is this Python code correct? Can I send handle directly like this?

推荐答案

如果确实存在一个名为"DMCap"的窗口,则您的代码应该可以按原样运行.要获取句柄和标题的列表,请运行以下代码:

Your code should run just fine as-is, IF there is truly a window titled "DMCap." To get a list of handles and titles, run the code below:

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

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

This will produce a list of tuples containing handle, title pairs.

这篇关于Python:win32gui.SetForegroundWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆