I wrote a python code for getting random text into a .txt file. Now I want to send this random text into notification area via 'notify-send' command. How do we do that?
I wrote a python code for getting random text into a .txt file. Now I want to send this random text into notification area via 'notify-send' command. How do we do that?
We can always call notify-send as a subprocess, e.g like that:
Alternatively we could also install python-notify2 or python3-notify2 and call the notification through that:
python3
Whilst you can call
notify-send
viaos.system
orsubprocess
it is arguably more consistent with GTK3 based programming to use the Notify gobject-introspection class.A small example will show this in action:
To answer Mehul Mohan question as well as propose the shortest way to push a notification with title and message sections:
Putting this in function might be a bit confusing due to quotes in quotes
You should use notify2 package, it is a replacement for python-notify. Use it as followed.
And the code:
For anyone looking at this in +2018, I can recommend the notify2 package.
PyNotify2, suggested by many answers, considers itself as deprecated as of late 2020:
So, given the above suggestions:
notify-send
subprocess approach is already explained in other answers, and py-notifier can simplify that, with an added bonus of working on Windows platforms using win10toast, but also with all the drawbacks of a subprocess call under the hood:PyNotify2
, and hasdbus-next
as its sole dependency.gi
introspection route. But please note he uses a different API than the one mentioned above:Gio.Notification
API, from Gio 2.4 onwards, mentioned bypynotify2
Notify
API, from GLib 2.0 onwards, used in @fossfreedom's code snippet.