2017-03-01 382 views
0

我没有从列表中选择一个项目(使用代码波纹管),我现在需要发送一个ctrl+E。问题是,SendKeys方法isn't available,我不能使用SendKeys('^e')。 (此快捷方式将在同上应用程式编辑所选择的项目)pywinauto:如何SendKeys不接受SendKeys的ListView?

from pywinauto.application import Application 
from pywinauto import findbestmatch 
from pywinauto import keyboard #not sure if I need to import it 


ditto=Application().connect(path='Ditto.exe') 

#---- print all available methods of the object 
print(dir(ditto.ditto.SysListView321.wrapper_object())) #(the list does not contains 'SendKeys') 


#-----Find and select the item (containing 'xxx') in the SysListView321 
#The list of texts to search through 
texts = ditto.ditto.SysListView321.texts()[1:] #skip window text itself, use only item texts 

# The list of items corresponding (1 to 1) to the list of texts to search through. 
items = ditto.ditto.SysListView321.items() #>>[]  
found_item = findbestmatch.find_best_match('xxx', texts, items, limit_ratio=0.1).Select() 

一些错误:

ditto.ditto.SysListView321.SendKeys('^e') 

... WindowSpecification class has no 'SendKeys' method

ditto.ditto.SysListView321.keyboard.SendKeys('^e') 

... findbestmatch.MatchError: Could not find 'keyboard' in 'dict_keys(['', 'Header'])'

[编辑](更多错误)

ditto.ditto.SysListView321.type_keys('^e') 

win32gui.SetForegroundWindow(self.handle) pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')

keyboard.send_keys('^e') 

AttributeError: module 'pywinauto.keyboard' has no attribute 'send_keys'


(诗。对于初学者:app.Ditto相当于app.window(best_match='Ditto')

回答

1

对于指定的UI元素此方法是

# it will activate target window if it's not in focus 
ditto.ditto.SysListView321.type_keys('^e') 

keyboard.SendKeys('^e') # should work also if you don't change active window 

它可以而不结合任何具体的控制中使用。

因此,您不应该尝试使用模块名称(如keyboard)作为任何对象的属性名称。它是Python。只要学习Python基础知识,你就会更好地理解pywinauto。

+0

谢谢!关于我的问题,你的解决方案没有任何工作(但它越来越好!)。我在我的问题编辑中添加了新的控制台错误。 ditto.ditto.SysListView321.type_keys('^ e')并不遥远:如果我在同一时间多次点击同上窗口,我会启动代码,它会起作用。所以它似乎是一个活动窗口的问题。脚本在同上列表中选择一个项目,但在type_keys('^ e')期间似乎失去焦点。 –

+0

我不明白你使用'.keyboard'的意思:我使用'keyboard.SendKeys'与我使用'findbestmatch.find_best_match'(用于访问类中的方法)相同的方式。 'SendKey's是'keyboard'类的一种方法吗?键盘是一个模块和键盘吧?如果是这样,为什么不把它们作为python调用呢:'class.method'? http://pywinauto.readthedocs.io/en/latest/code/code.html#main-user-modules –

+0

“keyboard”是一个模块,它不是另一个类的成员。你有没有使用其他编程语言的类? –

1

完成瓦西里的回答。这里是编辑单个同上项目所需的代码(它可以在大多数时间运行)

from pywinauto import findbestmatch 
from pywinauto.application import Application 
from pywinauto import remote_memory_block 
from pywinauto import keyboard 
from pywinauto import timings 
import time #needed for time.sleep(3) 


keyboard.SendKeys('^*') # custom shortcut to launch the specific ditto "group" 
time.sleep(2) # wait 2 sec for the app 

ditto=Application().connect(path='Ditto.exe') 
time.sleep(0.5) 

##find & select item 

#The list of texts to search through: 
texts = ditto.ditto.SysListView321.texts()[1:] #skip window text itself 

# The list of items corresponding (1 to 1) to the list of texts to search through. 
items = ditto.ditto.SysListView321.items() #>>[] 

found_item = findbestmatch.find_best_match('test', texts, items, limit_ratio=0.1).Select() 

## Extra: open the item in editor 
# Bring the window to the foreground first 
ditto.ditto.set_keyboard_focus() # (work also with set_focus but it remove the cursor) 

# edit the selected entry (it's a shortcut) 
keyboard.SendKeys('^e') 

# Wait (for the windows to load) 
time.sleep(1) # 1 sec 

# Select all 
keyboard.SendKeys('^a') 

ditto.Editor.close()