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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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