0

我确实从列表中选择了一个项目(使用下面的代码),我现在需要发送一个ctrl+E. 问题是 SendKeys 方法不知何故不可用,我无法使用SendKeys('^e')。(此快捷方式将在同上应用程序中编辑所选项目)

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 类没有“SendKeys”方法

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

... findbestmatch.MatchError:在'dict_keys(['','Header'])'中找不到'keyboard'

[编辑](更多错误)

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

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

 keyboard.send_keys('^e')

AttributeError:模块 'pywinauto.keyboard' 没有属性 'send_keys'


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

4

2 回答 2

2

对于指定的 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 基础知识,您也会更好地理解 pywinauto。

于 2017-03-03T11:24:02.203 回答
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()
于 2017-03-14T13:24:34.613 回答