简体   繁体   English

pywinauto:如何在不接受SendKeys的ListView上发送SendKeys?

[英]pywinauto: how to SendKeys on a ListView which doesn't accept SendKeys?

I did select an item from a list (using the code bellow), I'm need now to send a ctrl+E . 我确实从列表中选择了一个项目(使用下面的代码),现在需要发送ctrl+E The problem is that somehow the SendKeys method isn't available , I can't use SendKeys('^e') . 问题是,SendKeys方法不可用 ,我无法使用SendKeys('^e') (This shortcut will edit the selected item in the ditto app) (此快捷方式将编辑同上应用程序中的选定项目)

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()

Some Errors: 一些错误:

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

... WindowSpecification class has no 'SendKeys' method ... WindowSpecification类没有“ SendKeys”方法

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

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

[EDIT] (More errors) [编辑](更多错误)

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

win32gui.SetForegroundWindow(self.handle) pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available') win32gui.SetForegroundWindow(self.handle)pywintypes.error:(0,'SetForegroundWindow','没有可用的错误信息')

 keyboard.send_keys('^e')

AttributeError: module 'pywinauto.keyboard' has no attribute 'send_keys' AttributeError:模块“ pywinauto.keyboard”没有属性“ send_keys”


(Ps. for beginners: app.Ditto is equivalent to app.window(best_match='Ditto') ) (针对初学者的提示: app.Ditto等同于app.window(best_match='Ditto')

For the specified UI element this method is 对于指定的UI元素,此方法是

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

but

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

It can be used without binding to any specific control. 无需绑定任何特定控件即可使用它。

So you shouldn't try using module name (like keyboard ) as an attribute name of any object. 因此,您不应尝试将模块名称(例如keyboard )用作任何对象的属性名称。 It's Python. 是Python。 Just learn Python basics and you will understand pywinauto better as well. 只要学习Python基础知识,您就会更好地理解pywinauto。

To complete Vasily's answer. 完成瓦西里的答案。 Here is the code needed to edit a single ditto item (it works... most of the time) 这是编辑单个同上项目所需的代码(大多数情况下都有效)

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM