开发者

Get text from popup window

开发者 https://www.devze.com 2023-03-02 21:39 出处:网络
I\'m trying to read the text from a popup window. The title is always the same.I\'ve managed to identify the hwnd and get the title with the code below, but I can\'t figure out how to read the cont

I'm trying to read the text from a popup window.

Get text from popup window

The title is always the same. I've managed to identify the hwnd and get the title with the code below, but I can't figure out how to read the contents.

import time

import win32gui, win32con


windows = []
def _MyCallback( hwnd, extra ):
  extra.append(hwnd)
win32gui.EnumWindows(_MyCallback, windows)

while True:
  window = win32gui.GetForegroundWindow()
  title = win32gui.GetWindowText(window)
  if title == 'Errors occurred':  print 'error window'
  time.sleep(1)

Here's the working version:

import time

import w开发者_如何学JAVAin32gui

while True:
  window = win32gui.GetForegroundWindow()
  title = win32gui.GetWindowText(window)
  if title == 'Errors occurred':
    control = win32gui.FindWindowEx(window, 0, "static", None)
    print 'text: ', win32gui.GetWindowText(control)
  time.sleep(1)


You will only be able to read this text programmatically if it is contained in a windowed control. You can easily check this with Spy++. Many GUI frameworks don't use windowed controls for their child controls, or only use windowed controls for some children.

If it is a windowed control then you can identify it by calling GetWindow() and walking the child structure (obviously you need to use the win32gui equivalent).


I don't have access to the framework or the error dialog you are using, so I can only say in general what you want.

You need the FindWindowEx function, and use it to find a control whose class name is 'static' (or whatever the class name of the control is). I imagine this would be the line:

control = win32gui.FindWindowEx(window, 0, "Static", 0)

That returns the handle to the control, and you can then use GetWindowText on that to get the text.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号