如何删除过时的 pynotify 通知?
我刚开始使用 python 并为自己编写了一个漂亮的小脚本,它使用 通过 pynotify 发出 gnome 通知,如下所示:
import pynotify
pynotify.init("Application")
alert = pynotify.Notification("Title", "Description")
alert.show();
这非常有效,但问题是,当我连续执行脚本两次时,第一个通知需要一段时间才能消失。之后显示第二个。由于当我第二次执行脚本时第一个已过时,因此我想在显示第二个(或替换它)之前以编程方式删除第一个。这可能吗?如果可能,如何实现?
通过一些上下文来理解为什么我需要这个:由于我经常将鼠标从左手切换到右手,或者反过来,我想要一个脚本来反转此偏好并在通知中告诉我“切换到左手”惯用手”和“改用右手”。
I just started with python and wrote myself a nice, small script that uses
gnome-notifications via pynotify, like this:
import pynotify
pynotify.init("Application")
alert = pynotify.Notification("Title", "Description")
alert.show();
This works great, but the thing is, when I execute the script twice in a row it takes a while for the first notification to go away. The second gets shown after that. Since the first one is obsolete when i execute the script for the second time, I want to remove the first one programmatically prior to showing the second (or replace it). Is this possible, and if it is, how?
A bit of context to understand why I need this: Since I often switch my mouse from left- to right-handed and the other way around, I want a script that just inverts this preference and tells me in a notification "switched to left-handed" and "switched to right-handed".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我搜索了一段时间,得出的结论是在这种情况下不可能。
您可以使用
Notification.update()
更新现有通知对象。但您无法从系统中查询现有的来修改或隐藏它们。可以通过序列化将对象存储在某处并恢复它以进行更新。但即便如此,您仍然必须知道通知的确切持续时间以及启动通知时的时间戳,因为无法测试通知是否仍然可见。如何使用
update()
的简短示例。仅供参考,因为 pynotify 文档对我来说几乎不存在:您必须在更新后调用
show()
。否则,更改将不会显示。Notification 对象中还有一个 close() 函数,但这对我没有任何作用(在 Linux/Gnome 上,可能取决于系统)。
I searched around for a while and came to the conclusion that it is not possible in this case.
You are able to use
Notification.update()
to update an existing notification object. But you can't query existing ones from the system to modify or hide them. It may be possible to store the object somewhere via serialization and restore it to update. But even then you still have to know the exact duration of the notification and the timestamp when you launched it, since there is no way to test if a notification is still visible.A short sample how to use
update()
. Just for reference, since the pynotify doc seems almost non-existent to me:You have to call
show()
after the update. Otherwise the changes won't get displayed.There is also a close() function in the Notification object, but that doesn't do anything for me (on Linux/Gnome, may be system dependend).