Guilherme Matheus
Guilherme Matheus

Reputation: 595

Pywinauto: How to resize active window

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'

Upvotes: 5

Views: 2891

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

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.

Upvotes: 1

Related Questions