从 stcwindow 读取文本

Reading text from an stcwindow

我是一名尝试使用 pywinauto 自动化模拟过程的学生。模拟是 运行 在一个名为 DeltaEC 的程序上进行的,该程序是免费提供的。到目前为止,我已经使用 pywinauto 创建了打开、编辑和 运行 不同模拟的定义。下一步是读取模拟结果并存储它们,以便我稍后分析它们。我真的很难找到从我的程序中检索信息的方法。到目前为止,我已经以我能想到的所有方式使用了 .texts() ,但只能收到我想阅读的对话标题。我已经确定我想要的数据在'stcwindow'中。 Here is a screenshot of the window and the data I want to read

我想到了使用 right_click->select 然后 right_click-> 复制然后使用剪贴板模块检索数据的想法。然后我可以在进一步的代码中提取我需要的东西。

这是一个明智的做法吗?如果不可行,请您提出其他建议?

亲切的问候

弗朗西斯

很遗憾,Pywinauto 无法检索您想要的信息。事实上,Pywinauto 只识别了这个 GUI 的几个元素。

但是您的想法“right_click->select 然后 right_click->复制”是一个可行的方法。您只需确定要复制的区域。你可以用 Pywinauto 做到这一点。这是由 Pywinauto_recorder 生成的代码,通过一些小的改进来获取您想要检索的数据:

# encoding: utf-8
import pyperclip
import os, sys
script_dir = os.path.dirname(__file__)
sys.path.append(script_dir)
from pywinauto_recorder.player import *

PlayerSettings.mouse_move_duration = 0

with Window(u"Highlighted Params:Bottle0.OUT||Window"):
    right_click(u"stcwindow||Pane")

with Window(u"Context||Menu"):
    left_click(u"Select All||MenuItem")

with Window(u"Highlighted Params:Bottle0.OUT||Window"):
    right_click(u"stcwindow||Pane")

with Window(u"Context||Menu"):
    left_click(u"Copy||MenuItem")

result = pyperclip.paste()
print(result)

我做了以下修改:

  • 我添加了 PlayerSettings.mouse_move_duration = 0 以消除中间的鼠标移动
  • 我删除了搜索元素内的相对坐标(检测到的元素内距离边缘的百分比),因为您不需要它们。 使用 Nothing 或 %(0,0) 单击元素的中心,使用 %(-100.0) 它将单击元素左边缘的中心,使用 %(100.0) 它将点击元素右边缘的中心 等...
  • 我添加了两行使用 pyperclip.paste() 打印结果

Pywinauto_recorder 以与 Pywinauto 相同的方式识别元素,并且还允许使用各种策略来消除元素列表的歧义。 在您的情况下,唯一标识 DeltaEC 元素没有问题(即使可以访问的元素很少,鼠标悬停的所有区域都是绿色的)。

唯一标识一个元素Pywinauto_recorder根据被搜索元素的父元素的名称和类型构造一个路径。它从最高级别的父元素开始,然后是它的后代元素,并在层次结构中继续,直到到达搜索元素。

# encoding: utf-8

import os, sys
script_dir = os.path.dirname(__file__)
sys.path.append(script_dir)
from pywinauto_recorder.player import *

with Window(u"Highlighted Params:Bottle0.OUT||Window"):
    left_click(u"stcwindow||Pane")
    right_click(u"stcwindow||Pane")
with Window(u"Context||Menu"):
    wrapper = move(u"Select All||MenuItem")
    wrapper.draw_outline()
    print(wrapper.window_text())

我做了以下修改:

  • 我删除了相对坐标(检测到的元素内的百分比),因为您不需要它们。
  • 我添加了两行使用 Pywinauto 函数 'draw_outline()' 和 'window_text()'。

一些元素动态显示。菜单项就是这种情况。请注意,要访问 'Select All' 项,我调用 'move' 函数而不是 'left_click' 以免关闭菜单。

前面的代码仅适用于输入文件 'Bottle0.OUT' 如果您想让它适用于所有输入文件:

# encoding: utf-8

import os, sys
script_dir = os.path.dirname(__file__)
sys.path.append(script_dir)
from pywinauto_recorder.player import *

with Window(u"Highlighted Params:.*||Window", regex_title=True):
    left_click(u"stcwindow||Pane")
    right_click(u"stcwindow||Pane")
with Window(u"Context||Menu"):
    wrapper = move(u"Select All||MenuItem")
    wrapper.draw_outline()
    print(wrapper.window_text())