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

推荐订阅源

V
Visual Studio Blog
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Securelist
博客园 - Franky
P
Proofpoint News Feed
量子位
雷峰网
雷峰网
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Latest news
Latest news
L
Lohrmann on Cybersecurity
W
WeLiveSecurity
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
宝玉的分享
宝玉的分享
GbyAI
GbyAI
小众软件
小众软件
M
MIT News - Artificial intelligence
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
The Cloudflare Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
Scott Helme
Scott Helme
O
OpenAI News
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
K
Kaspersky official blog
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
腾讯CDC
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tailwind CSS Blog

博客园 - 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