pynotify: “关闭”信号回调从未触发
我正在尝试使用 pynotify(libnotify 的 Python 绑定)编写一个程序。我想在某个时候提出一个关键通知,并随着信息的变化每隔几秒更新一次,直到用户点击它。除了处理用户关闭它时发生的情况之外,这一切都有效。
为了更新通知,我需要在 Notification.update
之后调用 Notification.show
。这很好,但这意味着我需要跟踪用户是否已忽略通知,否则它会再次弹出。
我能想到的两种方法应该可以实现这一点:
- 检测通知是否可见。我还没有找到任何方法来找出这一点。
- 通知关闭时存储一些变量,然后在更新并再次调用
Notification.show
之前检查它。
第二种方法应该是可行的。我发现的示例代码(似乎没有任何适当的 pynotify 文档)导致我调用 Notification.connect
将 “关闭”
信号连接到回调。我尝试这样做,但回调从未被触发。
我用Google搜索并调试了很长时间但无法取得任何进展。最终我找到了一些 pynotify 附带的示例脚本。其中之一将一个处理程序附加到“close”信号:test-xy-stress.py
其内容如下:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gobject
import gtk
import gtk.gdk
import pynotify
import sys
import random
exposed_signal_id = 0
count = 0
def handle_closed(n):
print "Closing."
def emit_notification(x, y):
n = pynotify.Notification("X, Y Test",
"This notification should point to %d, %d." % (x, y))
n.set_hint("x", x)
n.set_hint("y", y)
n.connect('closed', handle_closed)
n.show()
def popup_random_bubble():
display = gtk.gdk.display_get_default()
screen = display.get_default_screen()
screen_x2 = screen.get_width() - 1
screen_y2 = screen.get_height() - 1
x = random.randint(0, screen_x2)
y = random.randint(0, screen_y2)
emit_notification(x, y)
return True
if __name__ == '__main__':
if not pynotify.init("XY Stress"):
sys.exit(1)
gobject.timeout_add(1000, popup_random_bubble)
gtk.main()
我运行了这个,发现这里的回调也从不触发。
这可能只是我的系统问题,还是 pynotify 或 libnotify 中的某个地方存在错误?如果现在无法解决这个问题,那么上面的选项 1 怎么样?有什么办法可以做到这一点吗?
我似乎有 libnotify 0.4.5 和 pynotify 0.1.1。
I'm trying to write a program with pynotify, the Python bindings for libnotify. I want to bring up a critical notification at some point and have it updated every few seconds as information changes, until the user clicks it away. This all works except handling what happens when the user dismisses it.
In order to update a notification I need to call Notification.show
after Notification.update
. That's fine, but it means that I need to keep track of whether the user has dismissed the notification, otherwise it'll just keep popping up again.
That ought to be possible by two methods I can think of:
- Detecting whether the notification is visible or not. I haven't found any way of finding that out.
- Storing some variable when the notification is closed, then checking it before updating and calling
Notification.show
again.
This second method should be possible. Example code I found (there doesn't seem to be any proper documentation for pynotify) led me to call Notification.connect
to connect a "closed"
signal to a callback. I tried to do that but the callback was never fired.
I Googled and debugged for a long time but couldn't make any progress. Eventually I found some example scripts which come with pynotify. One of them attaches a handler to the "closed"
signal: test-xy-stress.py
Its contents are as follows:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gobject
import gtk
import gtk.gdk
import pynotify
import sys
import random
exposed_signal_id = 0
count = 0
def handle_closed(n):
print "Closing."
def emit_notification(x, y):
n = pynotify.Notification("X, Y Test",
"This notification should point to %d, %d." % (x, y))
n.set_hint("x", x)
n.set_hint("y", y)
n.connect('closed', handle_closed)
n.show()
def popup_random_bubble():
display = gtk.gdk.display_get_default()
screen = display.get_default_screen()
screen_x2 = screen.get_width() - 1
screen_y2 = screen.get_height() - 1
x = random.randint(0, screen_x2)
y = random.randint(0, screen_y2)
emit_notification(x, y)
return True
if __name__ == '__main__':
if not pynotify.init("XY Stress"):
sys.exit(1)
gobject.timeout_add(1000, popup_random_bubble)
gtk.main()
I ran this and found that the callbacks here never fire either.
Could this be just my system, or is there a bug in pynotify or libnotify somewhere? If this is something beyond help right now, what about option 1 above -- is there any way to do that?
I seem to have libnotify 0.4.5 and pynotify 0.1.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直在寻找同样的东西。我发现有人使用 gobject.MainLoop 而不是 gtk.main 来帮忙: linuxquestions .org。
我发现这对我有用:
I've been looking for the same thing. I found someone using gobject.MainLoop instead of gtk.main to help out: linuxquestions.org.
I found this worked for me:
尝试以下操作:
添加:
并在emit_notification的末尾:
(与OP讨论后更新:)
这应该会触发你的回调!这样您就可以与 dbus-monitor 一起测试您的 DBus-Server 是否执行了它应该执行的操作。 (来自OP的想法。)
这仍然不是您真正寻求的,但暂时至少解释了您对未发出信号的困惑。
您可能应该研究的是动作属性。我在这里发现了一些有趣的东西。似乎您可以直接在通知中与用户交互。
因为 pynotify 只是 DBus 通信的包装器,所以您也可以尝试一种解决方法:
在定义处理程序之后:
出于测试目的,我还更改了函数的签名以适应 DBus 信号:
try the following:
add:
and at the end of your emit_notification:
(Updated after discussion with the OP:)
This should fire your callback! This way you can test together with dbus-monitor, if your DBus-Server does what it's supposed to do. (Idea from the OP.)
It's still not that what you are truly seeking, but for the time being explains at least your puzzlement about the unemitted signal.
What you probably should look into, are the action-properties. I found something interesting here. Seems like you can interact with the user directly in your notifications.
Because pynotify is just a wrapper for the DBus communications you might also try a workaround:
and after the definition of your handler:
i also changed for testing purposes the signature of your function to fit the DBus signal: