You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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通知模块,处理空值参数:

  1. 打开文件/usr/lib/python3.10/site-packages/plyer/platforms/linux/notification.py
  2. 找到_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:

  1. 安装库:
pip install notify2
  1. 测试代码:
import notify2

# 初始化应用名称
notify2.init('My Notification App')
# 创建并显示通知
notification = notify2.Notification(
    'Hello, world!',
    'Hello, world!'
)
notification.show()

内容的提问来源于stack exchange,提问作者Samuel de Oliveira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 06:35:17