惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
MyScale Blog
MyScale Blog
Jina AI
Jina AI
B
Blog
Microsoft Security Blog
Microsoft Security Blog
T
Troy Hunt's Blog
博客园_首页
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
T
Tenable Blog
B
Blog RSS Feed
S
Securelist
T
Threat Research - Cisco Blogs
P
Privacy International News Feed
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
AWS News Blog
AWS News Blog
V
V2EX
宝玉的分享
宝玉的分享
J
Java Code Geeks
小众软件
小众软件
Spread Privacy
Spread Privacy
腾讯CDC
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
V
Visual Studio Blog
The Hacker News
The Hacker News
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Know Your Adversary
Know Your Adversary
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
C
Check Point Blog
Webroot Blog
Webroot Blog
D
DataBreaches.Net
Cloudbric
Cloudbric
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家

博客园 - pmh905001

爬虫-今日头条我的收藏-反爬虫分析(六) pycharm的没落,vs code的兴起 爬虫-今日头条我的收藏-增量式下载网页内容(五) 爬虫-今日头条我的收藏-增量式导入到Elastic Search(四) 今日头条源代块一行代码很长情况下的拖动问题 爬虫-今日头条我的收藏-增量式导入到mongodb(三) 爬虫-今日头条我的收藏-增量式(二) openpyxl一个bug 爬虫-今日头条我的收藏(一) pyinstaller生成的exe程序使用使用默认程序打开execel文件 pyinstaller生成的exe文件的所在的工作目录问题 windows下python的keyboard库在锁屏之后再次登陆快捷键(热键)失效问题 pyinstaller 报错ImportError: No module named _bootlocale windows下gitbash 鼠标左键选中文字自动自行终止命令 ctrl+c ^C spring-security 如何使用用户名或邮箱登录 tomcat jndi context.xml的特殊字符转义问题 struts2的优缺点以及如何改造 jetty-maven-plugin 版本导致jetty启动失败问题 Eclipse下pom.xml的提示 Cannot access defaults field of Properties
pystray被隐藏菜单项显示出来的问题
pmh905001 · 2023-11-03 · via 博客园 - pmh905001

背景

pystray可以用来显示托盘,菜单项,以及气泡通知信息。如下界面,代码参考: https://gitee.com/pmh905001/shouyu/blob/main/shouyu/view/tray.py 。里面有一个功能是点击黄色的小黄鱼托盘图标打开excel的功能,这是默认的隐藏的菜单项。

由于有开机启动这个复选框,所以这是一个动态菜单,选中和非选中状态都会导致菜单状态发生变化。接着就发现了一个bug,点击开机启动菜单项的时候,会把隐藏的“显示Excel“ 菜单项显示出来,如下图所示:

 实际代码就是

icon.menu=(
MenuItem(text='帮助', action=cls.on_help),
MenuItem(text='设置', action=cls.on_config),
MenuItem(
text='开机启动',
action=cls.on_turn_off_auto_run if is_auto_run else cls.on_turn_on_auto_run,
checked=cls.display_checked,
),
MenuItem(text='重启', action=cls.on_restart),
MenuItem(text='显示Excel', action=cls.on_show, default=True, visible=False),
MenuItem(text='退出', action=cls.on_exit),
)

我的第一感觉是不是pystray的bug?因为我初始化菜单项也是传递一个tuple元组进去,动态菜单也只是修改了菜单的值并且进行了刷新。

pystray.Icon("name", Image.open(Package.get_resource_path('resources/icons/fish.png')), '授渔', cls._menu_items())

分析

经过查找,找到了错误的原因,https://github.com/moses-palmer/pystray/blob/master/lib/pystray/_base.py .托盘对象在初始化的时候,作者为了简化用户的传递,允许对menu简单传递一个元组(普通情况应该传递Menu对象),如下:

以上程序会把tuple对象封装成Menu对象。但请注意如下setter函数,作者似乎忘记了把tuple转换成Menu对象,直接进行了简单的赋值。

Menu本身也是可迭代对象,这就是为什么会导致  icon.menu=(item1,item2,)没有出现异常的原因。https://github.com/moses-palmer/pystray/blob/master/lib/pystray/_win32.py。  但是这样会导致隐藏的菜单项会被显示出来,也就是我所遇到的问题。

要解决该问题,实际上需要对setter函数进行修改,能够接受tuple或者Menu对象,就像在构造函数里面一样。

我的Pull request: https://github.com/moses-palmer/pystray/pull/152 

 如果不想修改pystray 的源代码,可以修改成如下:

menu_items=(
MenuItem(text='帮助', action=cls.on_help),
MenuItem(text='设置', action=cls.on_config),
MenuItem(
text='开机启动',
action=cls.on_turn_off_auto_run if is_auto_run else cls.on_turn_on_auto_run,
checked=cls.display_checked,
),
MenuItem(text='重启', action=cls.on_restart),
MenuItem(text='显示Excel', action=cls.on_show, default=True, visible=False),
MenuItem(text='退出', action=cls.on_exit),
)
icon.menu
= Menu(*menu_items)

总结:

  • 作者虽然简化用户的参数传递支持了元组类型,但是setter函数却很容易忘记了封装成合适的对象
  • Pull request: https://github.com/moses-palmer/pystray/pull/152