使用Python plyer库发通知遇D-bus TypeError,装依赖仍未解决
解决Arch Linux下Plyer D-bus通知TypeError问题
问题原因分析
错误提示TypeError: Expected a string or unicode object,核心原因是D-bus通知接口要求部分参数必须为字符串类型,但Plyer默认传入的app_name为空字符串,或app_icon传None时不符合接口规范。
解决方案
1. 显式指定app_name参数
调用notification.notify时添加非空的app_name参数,满足D-bus的字符串类型要求:
from plyer import notification if __name__ == "__main__": notification.notify( app_name='Python Notification', title='Hello, world!', message='Hello, world!', app_icon=None, timeout=10 )
2. 确保D-bus会话正常运行
若在纯终端(非桌面环境)执行脚本,需手动启动D-bus会话:
dbus-run-session -- python your_script.py
3. 修复Plyer的Linux通知实现
修改Plyer的Linux通知模块,处理空值参数:
- 打开文件
/usr/lib/python3.10/site-packages/plyer/platforms/linux/notification.py - 找到
_notify方法中的interface.Notify调用,将空值参数替换为合法字符串:
# 修改前 interface.Notify( self.app_name, 0, app_icon, title, message, [], {}, timeout ) # 修改后 interface.Notify( self.app_name or 'Plyer App', 0, app_icon or '', title, message, [], {}, timeout )
4. 替换为更可靠的通知库
如果Plyer问题持续,改用notify2库直接调用D-bus:
- 安装库:
pip install notify2
- 测试代码:
import notify2 # 初始化应用名称 notify2.init('My Notification App') # 创建并显示通知 notification = notify2.Notification( 'Hello, world!', 'Hello, world!' ) notification.show()
内容的提问来源于stack exchange,提问作者Samuel de Oliveira
相关产品推荐
相关产品推荐

