使用Python在Windows文件打开对话框(由桌面应用启动)中指定文件

时间:2019-07-16 18:41:51

标签: python win32gui

通过搜索诸如this之类的许多SO帖子,我能够提供以下代码来访问Windows File Open对话框。基本上,我需要在此对话框中指定一个文件,该文件由另一个应用程序打开(没有选项可以使用tKinter,而不是Web应用程序)。我收到以下错误

EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows)ctypes.ArgumentError:参数2::不知道如何转换参数2

我在这里想念什么?还有其他方法可以实现此功能吗?

import ctypes
import win32gui

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
SendMessage = ctypes.windll.user32.SendMessageW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
EnumChildWindows = ctypes.windll.user32.EnumChildWindows

WM_SETTEXT = 0x000C

def _windowEnumerationHandler(hwnd, resultList):
    #Pass to win32gui.EnumWindows() to generate list of window handle,
   # window text, window class tuples.
    resultList.append((hwnd, win32gui.GetClassName(hwnd)))

def searchChildWindows(currentHwnd,
                       wantedText=None,
                       wantedClass=None,
                       selectionFunction=None):
    childWindows = []
    result =[]
    EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows)
    for childHwnd, windowText, windowClass in childWindows:
        descendentMatchingHwnds = searchChildWindows(childHwnd)
        if wantedClass == windowClass:
            result.append(childHwnd)

    return childHwnd

def getEditBox(hwnd):
    children = list(set(searchChildWindows(hwnd, 'ComboBoxEx32')))
    for addr_child in children:
        if (win32gui.GetClassName(addr_child) == 'Edit'):
            print(f"found TextBox")
            SendMessage(hwnd, WM_SETTEXT, 0, "Can I change this title?")

def foreach_window_child(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)
        if (buff.value == "Open"):  # This is the window label
            print(f"foreach_window_child found it")
            #SendMessage(hwnd, WM_SETTEXT, 0, "Do u see me ") # Changes the title of dlg box
            getEditBox(hwnd)
    return True

EnumWindows(EnumWindowsProc(foreach_window_child), 0)

1 个答案:

答案 0 :(得分:0)

愚蠢的错误

EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows)

 EnumChildWindows(currentHwnd[0],_windowEnumerationHandler,childWindows)

解决了问题