0

I'm trying to understand how the findbestmatch module works. Here is an example.

from pywinauto.application import Application
from pywinauto.findbestmatch import find_best_match
ditto=Application().connect(path='Ditto.exe').window(title="Ditto",class_name="QPasteClass")
ditto.ditto.ListView.findbestmatch.find_best_match(hello)

I'm trying to use one of its method to get the HELLO 2 items listed inside ListView. (These items don't have their own controls identifiers)

print(ditto.print_control_identifiers()) gives that:

Control Identifiers:
QPasteClass - 'Ditto'    (L1114, T321, R1366, B740)
['QPasteClass', 'DittoQPasteClass', 'Ditto']
child_window(title="Ditto", class_name="QPasteClass")
   |
   | ListView - ''    (L1116, T343, R1357, B722)
   | ['ListView<noautodelete><ingroup><pasted>|HELLO 1\n','ListView<noautodelete><ingroup><pasted>|Hello 2\n', 'ListView<noautodelete><ingroup><pasted>|Hello 3\n', ]
   | child_window(class_name="SysListView32")
   |    |
   |    | Header - ''    (L1116, T343, R1357, B343)
   |    | ['Header', 'TagsHeader']
   |    | child_window(class_name="SysHeader32")
   |
   | Header - ''    (L1116, T343, R1357, B343)
   | ['Header', 'TagsHeader']
   | child_window(class_name="SysHeader32")

I tried ditto.ListView.findbestmatch.find_best_match("HELLO 2") and many others which did not work.

J. Does
  • 785
  • 3
  • 10
  • 23

1 Answers1

2

findbestmatch is a very low level module so usually it's used implicitly when calling attribute access (say app.Ditto and app.window(best_match='Ditto') are equivalent). But in your case using findbestmatch explicitly is necessary. Here is an example:

from pywinauto import findbestmatch
texts = ditto.ditto.ListView.texts()[1:] # skip window text itself, use only item texts
items = ditto.ditto.ListView.items()

found_item = findbestmatch.find_best_match('pasted', texts, items)
print(found_item)
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • Thanks a lot! I could now try to play with it to see how it works. I had no idea `app.Ditto` and `app.window(best_match='Ditto')` were equivalent. I will never cease to discover the basics of pywinauto ! – J. Does Feb 28 '17 at 15:34
  • @ I just added to your question `from pywinauto.application import Application` and `ditto=Application().connect(path='Ditto.exe')` (don't laugh: I struggled to find it!) – J. Does Feb 28 '17 at 15:37
  • just to be sure: when you say that `app.MyApp` and app.window(best_match='MyApp')` are equivalent, does it only apply to this specific example or is it a general rule in pywinauto? – J. Does Feb 28 '17 at 15:46
  • 1
    It's a general rule. :) Python makes it possible by overriding special attribute access method in a class. But if you need exact search just use window(title="MyApp"). There are many other possible criteria for a window specification object. – Vasily Ryabov Feb 28 '17 at 19:49
  • Thanks! (I will update my basic tuto with that http://stackoverflow.com/questions/42213490/pywinauto-how-to-select-this-dialog-which-spying-tool-to-use-what-information/42269661#42269661 ) – J. Does Feb 28 '17 at 19:57