鼠标悬停在图标上,并使用pywinauto和python获取工具提示文本

时间:2019-01-15 16:56:13

标签: python-3.x pywinauto

我想将鼠标悬停在第三方图标上(例如任务栏系统托盘中的“ CCleaner”),并在变量中获取工具提示文本。在pywinauto中,pywinauto.controls.GetTip(tooltip_index),pywinauto.controls.GetTipText(tooltip_index)和pywinauto.GetToolTipsControl()方法用于获取工具提示信息。但是我的代码中出现错误。 不确定如何获取optip_index。一段代码如下:

import pywinauto
from pywinauto import Application, taskbar

def click_tray_icon():
    app = Application(backend="uia").connect(path="explorer")
    sys_tray = app.window(class_name="Shell_TrayWnd")

    sys_tray.child_window(title="People").click_input(button='left', double=True)
    s = pywinauto.controls.common_controls.ToolTipsWrapper(sys_tray).texts()
    print(s)
    pywinauto.mouse.move(coords=(1729, 1063))

    pywinauto.controls.GetTip(1)
    pywinauto.controls.GetTipText(1)

AttributeError: module 'pywinauto.controls' has no attribute 'GetTip'

1 个答案:

答案 0 :(得分:0)

嗯,我有机会编写了一个示例脚本。但是它应该可以在当前的主分支上运行(0.6.6尚未发布)。因此,您必须通过以下方式安装pywinauto:

pip uninstall pywinauto
pip install https://github.com/pywinauto/pywinauto/archive/master.zip

该代码包含所有中间打印,以显示我如何编写它。每个下一步都包含来自先前child_window输出的粘贴复制的dump_tree规范。我刚刚删除了不必要的auto_id="1504"过滤器,因为我不确定这些值在所有Windows版本上都是相同的。

from __future__ import print_function
from pywinauto import Desktop

d = Desktop(backend='uia')

# print top level windows
# NOTE: method .windows() is not yet available for Desktop in pywinauto==0.6.5
for w in d.windows():
    print(w)

# print Taskbar subtree
d.Taskbar.dump_tree()

main_tray_toolbar = d.Taskbar.child_window(title="User Promoted Notification Area", control_type="ToolBar")

# print titles of main tray area
for text in main_tray_toolbar.texts():
    print(text)

notification_area_expand = d.Taskbar.child_window(title="Notification Chevron", control_type="Button")
notification_area_expand.invoke()
notification_area = d.window(title="Notification Overflow", control_type="Pane")

# print expanded area subtree
notification_area.dump_tree()

# print hidden icon texts
hidden_toolbar = notification_area.child_window(title="Overflow Notification Area", control_type="ToolBar")
for text in hidden_toolbar.texts():
    print(text)