Gyula
Gyula

Reputation: 73

Connecting to a second Application using pywinauto

I'm trying to use python to opperate several Applications. Currently I am able to open the first application and use a toolbar to open another Application.

import pywinauto
import os
os.startfile("Path")
app = pywinauto.application.Application(backend="uia") 
app.connect(path="path")
app.top_window().descendants(control_type="MenuBar")
app_menu = app.top_window().descendants(control_type="MenuBar")[1]
app_menu.items()
appmenu = app.top_window().descendants(control_type="MenuBar")[1]
mi = appmenu.items()[3]
mi.set_focus()
mi2 = app.top_window().descendants(control_type="MenuItem")[1]
mi2.set_focus()
mi2.select()

` This works so far. When I'm trying to obtain control over this new applicationI get an Error. TypeError: connect() missing 1 required positional argument: 'self' Code I use to connect to 2nd App:

app2 = pywinauto.application.Application(backend="uia")
app2= pywinauto.application.Application.connect(path="path2")

How can i connect to this 2nd application ?

Upvotes: 1

Views: 3616

Answers (1)

Dalen
Dalen

Reputation: 4236

You have a bug:

app2 = pywinauto.application.Application(backend="uia")
app2= pywinauto.application.Application.connect(path="path2")

Should be:

app2 = pywinauto.application.Application(backend="uia")
# Don't forget to start the 'path2' first or use app2.start() instead.
# Or, well, the first Application()'s automation should start it.
# Regardless, you would perhaps have to wait some time before app starts and then connect() will work.
# So put some time.sleep() here or setup a pywinauto's timeout.
app2.connect(path="path2")

Just like you did it for the first application.

The TypeError is raised because you have been calling connect() from the Application class directly, instead from the Application() instance. The connect() method was missing the 'self' reference as a first argument which is added automatically when you call a method from an instance's pointer.

This means that this would have the same effect:

app2 = pywinauto.application.Application(backend="uia")
pywinauto.application.Application.connect(app2, path="path2")

See, app2 is passed as the first (mandatory) positional argument. This way pywinauto.application.Application.connect() knows to which object (app2) it should bind the application's window. If you call it as app2.connect() it already gets the app2 so there is no need to pass it.

Upvotes: 4

Related Questions