Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 30 down vote accepted

We can always call notify-send as a subprocess, e.g like that:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import subprocess

def sendmessage(message):
    subprocess.Popen(['notify-send', message])
    return

Alternatively we could also install python-notify Install python-notify and call the notification through that:

import pynotify

def sendmessage(title, message):
    pynotify.init("Test")
    notice = pynotify.Notification(title, message)
    notice.show()
    return
share|improve this answer
    
Thank you very much –  bobbyaldol Feb 29 '12 at 12:35
    
+1 Great answer! –  César Feb 29 '12 at 14:42
1  
I had to use subprocess.Popen(['notify-send', message]) to make the first example work. –  lgarzo Feb 29 '12 at 16:52
    
The second one a pynotify.init("Test") and pynotify.Notification(title, message).show(). By the way I'm „Learning Python The Hard Way”, so I might just overlook something... –  lgarzo Feb 29 '12 at 17:07
    
So sorry for that. I was unable to test this earlier on my box at work - my bad. Edited the missing bits in. –  Takkat Feb 29 '12 at 17:21

python3

Whilst you can call notify-send via os.system or subprocess it is arguably more consistent with GTK3 based programming to use the Notify gobject-introspection class.

A small example will show this in action:

from gi.repository import Notify

class MyClass(GObject.Object):
    def __init__(self):

        super(MyClass, self).__init__()
        # lets initialise with the application name
        Notify.init("myapp_name")

    def send_notification(self, title, text, full_path_to_icon=""):

        n = Notify.Notification.new(title, text, file_path_to_icon)
        n.show()

my = MyClass()
my.send_notification("this is a title", "this is some text")
share|improve this answer
import os
mstr='Hello'
os.system('notify-send '+mstr)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.