2017-03-01 124 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()