【问题标题】:pywinauto: how to SendKeys on a ListView which doesn't accept SendKeys?pywinauto:如何在不接受 SendKeys 的 ListView 上发送 Keys?
【发布时间】:2017-03-01 17:58:53
【问题描述】:

我确实从列表中选择了一个项目(使用下面的代码),我现在需要发送ctrl+E。问题是不知何故 SendKeys 方法isn't available,我不能使用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', '没有可用的错误信息')

 keyboard.send_keys('^e')

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


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

【问题讨论】:

    标签: python python-3.x pywinauto 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 基础知识,您也会更好地理解 pywinauto。

    【讨论】:

    • 谢谢!关于我的问题,您的解决方案都没有奏效(但它正在变得更好!)。我在问题的编辑中添加了新的控制台错误。 ditto.ditto.SysListView321.type_keys('^e') 不远:如果我在启动代码的同时多次单击同上窗口,它可以工作。所以这似乎是一个活动窗口的问题。该脚本在同上列表中选择一个项目,但在 type_keys('^e') 期间似乎失去了焦点。
    • 我不明白你所说的使用 .keyboard 是什么意思:我使用 keyboard.SendKeys 的方式与使用 findbestmatch.find_best_match 的方式相同(访问类内的方法)。 SendKeys 是 keyboard 类的方法,对吧?键盘是模块和键盘太对了吗?如果是这样,为什么不像 python 那样调用这些方法:class.methodpywinauto.readthedocs.io/en/latest/code/…
    • "keyboard" 是一个模块,它不是另一个类成员。你曾经用过其他编程语言的类吗?
    • 操作,对不起!我的错。 keyboard.SendKeys('^e') 应该可以工作。当然,我们还需要有 PEP-8 兼容名称send_keys,但我们错过了它。稍后会添加。
    • 谢谢瓦西里!我确实使用SendKeys 做到了。但只是为了通知您ditto.ditto.SysListView321.type_keys('^e') 不起作用(它在 python 控制台上发送密钥:^E)。
    【解决方案2】:

    完成瓦西里的回答。这是编辑单个同上项目所需的代码(它工作......大部分时间)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2023-03-28
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2017-12-10
      相关资源
      最近更新 更多