Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

pywinauto - Reading text from an stcwindow

I am an student trying to automate a simulation process using pywinauto. The simulation is run on a program called DeltaEC which is freely available. I have used pywinauto so far to create defs that open, edit and run different simulations. The next step is reading the results of the simulation and storing them so I can analyse them later. I am really struggling to find ways of retrieving information from my program. So far I have used .texts() in all the ways I can think of, but can only receive the title of the dialogue I want to read. I have identified that the data I want is in a 'stcwindow'. Here is a screenshot of the window and the data I want to read

I had an idea to use right_click->select all then right_click->copy and then use the clipboard module to retrieve the data. I could then extract what I needed in further code.

Is this a sensible approach, and if not please could you suggest something else?

Kind regards

Francis

question from:https://stackoverflow.com/questions/65858233/reading-text-from-an-stcwindow

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately Pywinauto cannot retrieve the information you wish to have. Indeed Pywinauto identifies only few elements of this GUI.

But your idea "right_click->select all then right_click->copy" is a possible way to do it. You just have to identify the region to copy. You can do it with Pywinauto. Here is the code generated by Pywinauto_recorder to reach the data you want to retrieve with some little enhancements:

# 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)

I made the following modifications:

  • I added PlayerSettings.mouse_move_duration = 0 to eliminate intermediate mouse movements
  • I removed the relative coordinates inside the searched element (percentage inside the detected element from the edges) because you don't need them. With Nothing or %(0,0) to click in the centre of the element, with %(-100.0) it will click in the centre of the left edge of the element, with %(100.0) it will click in the centre of the right edge of the element etc...
  • I added two lines that use pyperclip.paste() to print the result

Pywinauto_recorder identifies elements in the same way as Pywinauto and also allows a list of elements to be disambiguated using various strategies. In your case there is no problem to uniquely identify DeltaEC elements (even if few elements are accessible, all areas hovered over by the mouse are coloured green).

To uniquely identify an element Pywinauto_recorder constructs a path based on the names and types of the parent elements of the searched element. It starts with the highest level parent element, then its descendant and continues in the hierarchy until reaching the searched element.

# 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())

I made the following modifications:

  • I removed the relative coordinates (percentage inside the detected element) because you don't need them.
  • I added two lines that use the Pywinauto functions 'draw_outline()' and 'window_text()'.

Some elements appear dynamically. This is the case for menu items. Note that to access the 'Select All' item, I call the 'move' function instead of 'left_click' so as not to close the menu.

The previous code works only with the input file 'Bottle0.OUT' if you want to make it work with all input files:

# 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())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...