Python: win32gui.SetForegroundWindow

天大地大妈咪最大 提交于 2019-12-12 09:18:05

问题


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"

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/16770909/python-win32gui-setforegroundwindow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!