Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pywinauto连接到第二个应用程序_Python_Python 3.x_Typeerror_Pywinauto - Fatal编程技术网

Python 使用pywinauto连接到第二个应用程序

Python 使用pywinauto连接到第二个应用程序,python,python-3.x,typeerror,pywinauto,Python,Python 3.x,Typeerror,Pywinauto,我正在尝试使用python来运行几个应用程序。 目前,我可以打开第一个应用程序,并使用工具栏打开另一个应用程序 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 = a

我正在尝试使用python来运行几个应用程序。 目前,我可以打开第一个应用程序,并使用工具栏打开另一个应用程序

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()
` 到目前为止,这是可行的。当我试图控制这个新应用程序时,我会得到一个错误。TypeError:connect()缺少1个必需的位置参数:“self” 我用于连接第二个应用程序的代码:

app2 = pywinauto.application.Application(backend="uia")
app2= pywinauto.application.Application.connect(path="path2")
如何连接到第二个应用程序?

您有一个错误:

app2 = pywinauto.application.Application(backend="uia")
app2= pywinauto.application.Application.connect(path="path2")
应该是:

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")
就像你在第一次申请时那样

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()
引发TypeError是因为您直接从应用程序类调用了connect(),而不是从Application()实例调用。connect()方法缺少作为第一个参数的“self”引用,当您从实例指针调用方法时,会自动添加该参数

这意味着这将产生相同的效果:

app2 = pywinauto.application.Application(backend="uia")
pywinauto.application.Application.connect(app2, path="path2")
请参见,app2作为第一个(必需)位置参数传递。这样pywinauto.application.application.connect()就知道应该将应用程序窗口绑定到哪个对象(app2)。如果将其称为app2.connect(),则它已获取app2,因此无需传递它