Python:如何从另一个程序窗口获取文本标签? [英] Python: How to get the text label from another program window?

查看:74
本文介绍了Python:如何从另一个程序窗口获取文本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Python 从另一个程序中读取文本标签.我想我必须使用 WM_GETTEXT,但我不知道如何使用,并且在互联网上找不到任何东西.我的程序获取活动窗口,但不读取文本标签.所以我希望有人可以帮助我.

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 gets the active window, but doesn't read the text labels. So I hope that somebody can help me.

我添加了缓冲区和 SendMessage 部分.例如,我可以从编辑器中获取文本,但不能从我试图从中获取文本标签的程序中获取.

I have added the buffer and SendMessage Part. I can get the text from Editor for example, but not from the program I am trying to get the text labels from.

我有以下代码,我在 stackoverflow(从弹出窗口获取文本):

I have the following code, which I found here on stackoverflow(Get text from popup window):

import win32gui
import time

while True:
    window = win32gui.GetForegroundWindow()
    title = win32gui.GetWindowText(window)
    if 'GLS Exportdatei' in title:
        control = win32gui.FindWindowEx(window, 0, 'static', None)
        buffer = win32gui.PyMakeBuffer(20)
        length = win32gui.SendMessage(control, win32con.WM_GETTEXT, 20, buffer)

        result = buffer[:length]
        print result
        time.sleep(1)

推荐答案

如果您的窗口文本超过 20 个字符,则说明您创建的缓冲区太小.尝试将其扩展到超出您可能需要的范围:

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)

如果您想访问主窗口中的控件,请使用 EnumChildWindows,传递父窗口的句柄.您可能需要递归执行此操作.

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.

这篇关于Python:如何从另一个程序窗口获取文本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆