简体   繁体   English

在 pywinauto 中,如何右键单击系统托盘中的应用程序图标以打开上下文菜单?

[英]In pywinauto, how can I right click my app icon in the system tray to open the context menu?

How can i right click the app's icon in the systemtray and select one of the popup menuitem using pywinauto?如何右键单击系统托盘中的应用程序图标并使用 pywinauto 选择弹出菜单项之一?

I have my app on the Windows Desktop systemtray,which can't be loaded using the .exe file.我的应用程序在 Windows 桌面系统托盘上,无法使用 .exe 文件加载。 So i have to right click on the systemtray icon and select one of the popup menu item in order to get the app's GUI.I was trying to achieve this using pywinauto using python 64 bit.所以我必须右键单击系统托盘图标并选择弹出菜单项之一才能获得应用程序的 GUI。我试图使用 pywinauto 使用 python 64 位实现这一点。

Here is my code.这是我的代码。

app = Application(backend="uia").connect(path="explorer")
sys_tray = app.window(class_name="Shell_TrayWnd")
loc = sys_tray.child_window(title='App name').click()

This is changing the mouse position to the required App's icon,but its not right clicking on that and i want to select one menu item from that pop up also.这是将鼠标位置更改为所需应用程序的图标,但它没有右键单击该图标,我也想从该弹出窗口中选择一个菜单项。 how can i get this?我怎么能得到这个?

There is method .click_input(button="right") which moves real cursor and performs real click.有方法.click_input(button="right")移动真正的光标并执行真正的点击。 In your case it would look so (on Windows 10 version 1803):在您的情况下,它看起来是这样(在 Windows 10 版本 1803 上):

#from __future__ import print_function
from pywinauto import Desktop

d = Desktop(backend='uia')
#d.Taskbar.dump_tree()
main_tray_toolbar = d.Taskbar.child_window(title="User Promoted Notification Area", control_type="ToolBar")
#print(main_tray_toolbar.texts())

icon = main_tray_toolbar.child_window(title_re="Cisco AnyConnect Secure Mobility Client.*", control_type="Button")
icon.click_input(button="right")

#d.ContextMenu.dump_tree()
d.ContextMenu.wait('visible', timeout=10) # flexibly wait up to 10 sec.
d.ContextMenu.child_window(title="About", control_type="MenuItem").invoke()

Helpful debug prints are commented (all .child_window specifications have been just copied from dump_tree() output).有用的调试打印被注释(所有.child_window规范都刚刚从dump_tree()输出中复制)。 There is also method d.windows() that is available for master branch only (pywinauto 0.6.6 is coming in nearest 2 weeks).还有一种方法d.windows()仅适用于 master 分支(pywinauto 0.6.6 将在最近 2 周内推出)。

Comment on Vasily's answer: because the names of the windows and toolbars are localized, the code will not work on systems whose locale is not English.对 Vasily 的回答发表评论:因为窗口和工具栏的名称是本地化的,该代码在语言环境不是英语的系统上不起作用。 I got this to work on a French Windows 10 system by replacing我通过替换使其在法国 Windows 10 系统上工作

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

with

main_tray_toolbar = d.window(class_name='Shell_TrayWnd').child_window(class_name='ToolbarWindow32', control_type="ToolBar")

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

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