我正在尝试使用 python 和 pywin32 从另一个应用程序导入/提取表单的值以在我的代码中使用。
通过 Spy++,我找到了需要定位的元素的句柄,但我不知道如何从元素中获取文本。
目标应用程序的结构方式:
top app level
(no name) class: MDIClient (this is the centerWidget)
(no name) class: ThunderRT6FormDC (occupies same space as centerwidget)
(Ticket) class: ThunderRT6Frame (Frame that holds all fields to extract from)
(no name) class: NumEdit/DateEdit/TextEdit... (fields to import)
(Information) class ThunderRT6Frame (2nd frame with fields)
(no name) class: NumEdit/DateEdit/TextEdit... (fields to import)
(Process) class ThunderRT6Frame (3rd frame with fields)
(no name) class: NumEdit/DateEdit/TextEdit... (fields to import)
f = win32gui.FindWindow('ThunderRT6MDIForm', 'ApplicationName')
ex = win32gui.FindWindowEx(f, 0, 'MDIClient', None)
exx = win32gui.FindWindowEx(ex, 0, 'ThunderRT6FormDC', None)
exxx = win32gui.FindWindowEx(exx, 0, 'ThunderRT6Frame', None)
exxxx = win32gui.FindWindowEx(exxx, 0, 'NumEdit', None)
exxxx 是我要从中提取标题/文本的句柄/元素。 我确信 FindWindowEx 不是正确的函数,因为它搜索 Windows,但我必须使用什么函数?
最佳答案
[MS.Docs]: FindWindowExW function (由 win32gui.FindWindowEx
包装)应该可以解决问题,如果树中 2 个“连续”窗口之间的关系(例如 ex 和 exx ) 是父 - 子之一。
另一种方法是使用 [MS.Docs]: EnumChildWindows function (win32gui.EnumChildWindows
) - 还要检查EnumWindows。
欲了解更多详情,您可以查看:
- [SO]: Get the title of a window of another program using the process name (@CristiFati's answer)
- [SO]: Keyboard event not sent to window with pywin32 (@CristiFati's answer)
从那里,您可以使用GetWindowTextW (win32gui.GetWindowTextW
) 获取所需窗口的标题。
关于python - 从另一个应用程序的表单/字段获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56800217/