问题
I am attempting to use pynotify for a small project, but am having a strange problem on my Fedora 13 machine. It appears that when pynotify is run programmatically it crashes when show()
is called - however if I type that line myself it runs fine! I have tested it also on my Ubuntu box, where it runs absolutely fine.
My testing code is:
import pynotify
pynotify.init('someName')
n = pynotify.Notification("Summary", "Body")
n.show()
And the results of running this:
$ python -i test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
n.show()
glib.GError: Unable to connect to server
>>> n.show()
True
>>>
So, does anyone have any ideas what may cause this sort of behaviour? Unfortunately the Fedora environment is one that I have little control over, so any solutions requiring root access/etc would not really work. I can try installing pynotify locally, however, if needed. Thanks for any help.
回答1:
Since Owen has not accepted my offer to take this answer, here is the solution that worked for me. Note that I have no idea why this works (other than vague guesses), and that I don't make any sort of guarantees about whether this is a good solution or not, but maybe if you are ever in as odd a position as I was this will help.
If you execute n.show()
twice, it will run successfully the second time. Therefore, in order to avoid setting two notifications on a system where Pynotify does work correctly, I have used the following:
import pynotify
pynotify.init('someName')
n = pynotify.Notification("Summary", "Body")
try:
n.show()
except:
n.show()
Note of course that this small example has flaws, least of all the outcome if there is an actual problem with Pynotify that will be thrown on both n.show()
s - this is merely a minimum working example.
来源:https://stackoverflow.com/questions/5623289/pynotify-runs-fine-interactively-but-crashes-when-run-programmatically-on-fedo