3

I am trying to return all windows from my machine using PyAutoWin. The ultimate goal is to later narrow down this list to a subset of windows to automate (resize and perform scraping actions) using a mixture of methods.

However I am failing at the most basic task: returning all windows. My code is:

import pywinauto

print(pywinauto.findwindows.enum_windows())

and getting this error:

Traceback (most recent call last):
  File "app.py", line 4, in <module>
    print(pywinauto.findwindows.enum_windows())
  File "C:\Users\*\.virtualenvs\scraper-j58Iv-wO\lib\site-packages\pywinauto\findwindows.py", line 368, in enum_windows
    win32functions.EnumWindows(proc, 0)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected WinFunctionType instance instead of WinFunctionType

I tried instantiating a Desktop() object and passing it to the enum_windows() method but it does not take in argument.

Any help is greatly appreciated!

Best regards

fisheatshark
  • 145
  • 1
  • 5
  • This looks a little bit crazy to me: `expected WinFunctionType instance instead of WinFunctionType`. But anyway we don't recommend using low level functions. It's highly recommended to learn the core concept in the [Getting Started Guide](https://pywinauto.readthedocs.io/en/latest/getting_started.html) and then some advanced guides around it. – Vasily Ryabov Jul 26 '19 at 10:40
  • Vasily, thank you very much for your reply. I found it hard to get started from there since the guide is predominantly written to interact with a single application whereas I need to identify windows first and only then start the automation process. Thanks nonetheless, I will give this another, much more thorough, read tonight. – fisheatshark Jul 26 '19 at 11:13

1 Answers1

8

The right way to do that:

from pywinauto import Desktop
top_windows = Desktop(backend="uia").windows() # or backend="win32" by default

# walk the returned list of wrappers
for w in top_windows:
    print(w.window_text())
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • And thanks again, this did exactly what I was looking for. Now onto my next challenge, finding the size and coordinates of each active window :-) – fisheatshark Jul 26 '19 at 11:14
  • 3
    `w.rectangle()` – Vasily Ryabov Jul 26 '19 at 11:20
  • I just got the same problem as OP but I'm not really interested in your solution, as I'm trying to figure out all the functionalities of pywinauto. Why doesn't OP's line of code works, and what does it mean? In the doc it says "Return a list of handles of all the top level windows", but it obviously doesn't do it. Is pywinauto garbage and I should try to find an other module? – user3548298 Nov 22 '19 at 00:06
  • 1
    @user3548298 if you're new to pywinauto, you should start from the [Getting Started Guide](https://pywinauto.readthedocs.io/en/latest/getting_started.html). OP is using very low level API which is not recommended. I mean `findwindows.enum_windows()`: it really returns list of handles. But method `.windows()` of the `Application` or `Desktop` object returns a list of high level wrappers. – Vasily Ryabov Nov 23 '19 at 06:06