Python: How to get the text label from another program window?

前端 未结 2 1750
清歌不尽
清歌不尽 2021-02-06 16:45

I want to read text labels from another program, with Python. I think I have to use WM_GETTEXT, but I don\'t know how and I couldn\'t find anything on the internet. My program g

相关标签:
2条回答
  • 2021-02-06 17:11

    If the text of your window has more than 20 characters, then the buffer you have created is too small. Try expanding it to more than you're likely to need:

    buffer = win32gui.PyMakeBuffer(255)
    length = win32gui.SendMessage(control, win32con.WM_GETTEXT, 255, buffer)
    

    If you want to get to the controls within the main window, then use EnumChildWindows, passing the handle of the parent window. You may need to do this recursively.

    0 讨论(0)
  • 2021-02-06 17:29

    win32gui.PyMakeBuffer has been deprecated. Also, buffer is a builtin function, so don't use it as a variable name.

    Instead, just do this:

    buf = " " * 255
    length = win32gui.SendMessage(control, win32con.WM_GETTEXT, 255, buf)
    result = buf[:length]
    
    0 讨论(0)
提交回复
热议问题