6

I'm trying to automate some windows tasks and I got a dataframe of all windows opens, and then I added some more coluns in order to make some validations before proceed with the automation.

After I activate a window from set_focus() function, I'm not beeing able to resize any window at all, simply nothing happens.

I already tried using win32gui solution:

current = win32gui.GetForegroundWindow()
win32gui.MoveWindow(current, 0, 0, 100, 100, True)

I also tried using pygetwindow functions to resizeTo or size, but nothing happens as well.

If I run the following command app.move_window(x=None, y=None, width=100, height=100, repaint=True): AttributeError: 'UIAWrapper' object has no attribute 'move_window'.

My code:

from pywinauto import Desktop
import pandas as pd

windows = Desktop(backend="uia").windows()
window = [w.window_text() for w in windows]

# Create a dataframe in order to store the windows needed
df_windows = pd.DataFrame(window, columns =['WebBrowser'])
# Filter dataframe only to show all windows from Brave web browser
df_windows = df_windows.loc[df_windows['WebBrowser'].str.contains("Brave:", case=False)]
# Add column profile from Brave
df_windows['Profile'] = df_windows['WebBrowser'].str.split(':').str[1].str.strip()
# Add column window open from Brave
df_windows['Window'] = df_windows['WebBrowser'].str.split(':').str[0].str.strip()
# Add column about the website open from Brave
df_windows['Website'] = df_windows['Window'].str.replace(" - Brave", "").str.strip()
# Filter dataframe only to show all bombcrypto game window
df_windows = df_windows.loc[df_windows['Website'] == 'GuilhermeMatheus']

print(df_windows)

for x in df_windows['WebBrowser']:
    print(x)
    app = Desktop(backend="uia").windows(title=x)[0]
    app.set_focus()
    app.move_window(x=None, y=None, width=100, height=100, repaint=True)

How can I resize the active window after set_focus()?

[EDIT]

Following @Vasily Ryabov tips, I downloaded Windows SDK in order to install the feature that has Inspect.exe, also following the documenation from pywinauto from Getting Start topic and from the documentation available at Accessibility tools - Inspect I was able to find my application with Inspect and the Resize options is available as evidence below:

enter image description here

So I tried the following commands:
1º:

app.iface_transform.Resize(200, 600)  

The error:

app.iface_transform.Resize(200, 600)
_ctypes.COMError: (-2146233079, None, (None, None, None, 0, None))

2º:

app.Resize(200, 600)  

The error:

AttributeError: 'UIAWrapper' object has no attribute 'Resize'

3º:

app.wrapper_object.Resize(200, 600)  

The error: AttributeError: 'UIAWrapper' object has no attribute 'wrapper_object'

Guilherme Matheus
  • 573
  • 10
  • 30

1 Answers1

1

I'd suggest to try these methods for UIAWrapper objects (assuming app is a UIAWrapper instance). In your code it should look like:

app.iface_transform.Move(x, y)
app.iface_transform.Resize(width, height)

To understand if this TransformPattern is available, let's check it with Inspect.exe in "Action" menu for the selected window:

transform pattern in Action menu

In next major release (it's not out yet) we implemented WindowWrapper that contains method move_window which uses above methods of TrasformPattern.

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • Hi @Vasily, thank you for answering and explaining. I'm still facing errors though: `AttributeError: 'UIAWrapper' object has no attribute 'wrapper_object'` – Guilherme Matheus Dec 26 '21 at 14:31
  • Oh, sorry, just skip `.wrapper_object()` call in the chain. Also if transform pattern is not implemented on application side, you will get exception `NoPatternInterfaceError`. – Vasily Ryabov Dec 26 '21 at 18:04
  • I really don't know what is the problem that I'm still getting: `app.iface_transform.Resize(300, 300) _ctypes.COMError: (-2146233079, None, (None, None, None, 0, None))`. Sorry to bother, can you help me? If I understood correctly my app is a `UIAWrapper` because I created again from the dataframe by calling from `app = Desktop(backend="uia").windows(title=x)[0]` (the `set_focus()` even work) – Guilherme Matheus Dec 27 '21 at 02:21
  • Maybe you need only `Resize`. Does it work for other top level windows? I mean items of the list returned by method `.windows()`. It returns all top level windows if parameter is not provided. Also I'd recommend to check if Inspect.exe "Action" menu items work for this window and for others. – Vasily Ryabov Dec 27 '21 at 16:42
  • I tried running `Inspect.exe` from https://github.com/pywinauto/py_inspect but I don't have the options bar pane, so I'm not able to see `Actions`. I also tried on items of the list from `.windows()`, if I try `app.iface_transform.Resize(width, height)` I get the error `NoPatternInterfaceError` as you said previously, but I don't know why exactly. How do I know if transform pattern is implemented? But either way I get different error from `app` as I said in my last answer: `app.iface_transform.Resize(300, 300) _ctypes.COMError: (-2146233079, None, (None, None, None, 0, None))`. – Guilherme Matheus Dec 27 '21 at 17:03
  • py_inspect is not the same as inspect.exe which is part of Windows SDK or can be downloaded. The link is provided in the Getting Started Guide of pywinauto which is highly recommended to read. – Vasily Ryabov Dec 29 '21 at 09:39
  • That's true I wans't checking in the right place, but I read the documentation and updated my question with the evidences, can you take a look please? The `Transform.Resize` is available at `Inspect`. – Guilherme Matheus Jan 12 '22 at 20:47
  • If "Transform.Resize" is not working in Inspect.exe, it won't work with pywinauto as well because it's the app issue probably or an issue in system UIAutomationCore.dll (less probably). The code `app.iface_transform.Resize(200, 600)` looks correct for your case. But it looks like an error on the app side. Please double check it in Inspect.exe "Actions" menu item. – Vasily Ryabov Jan 15 '22 at 10:41