samedi 22 septembre 2012

Notifications en Python

Après les mails, voyons les notifications gtk en python. Elles sont simples à mettre en place et sont un petit plus. Maintenant que vous savez checker vos mails, vous pourrez être averti lorsque vous en recevez.

Il convient d'abord d'initialiser la librairie pynotify.
try:
    import gtk
    import pynotify
    pynotify.init( "monApplication" )
except:
    print "Vous n'avez pas pynotify"

Une fois cette vérification faite, vous pouvez créer votre fonction de notification comme la suivante:




def notify(phrase):
    temp=phrase.split(";")
    title=temp[1]
    body=temp[2]
    type=temp[3]
    n = pynotify.Notification(title,body)
    if (type=="1"):
        n.set_icon_from_pixbuf(gtk.Label().render_icon(gtk.STOCK_NETWORK, gtk.ICON_SIZE_LARGE_TOOLBAR))
        n.set_urgency(pynotify.URGENCY_LOW)
    elif (type=="2"):
        n.set_icon_from_pixbuf(gtk.Label().render_icon(gtk.STOCK_DIALOG_INFO, gtk.ICON_SIZE_LARGE_TOOLBAR))
        n.set_urgency(pynotify.URGENCY_LOW)
    elif (type=="3"):
        n.set_icon_from_pixbuf(gtk.Label().render_icon(gtk.STOCK_APPLY, gtk.ICON_SIZE_LARGE_TOOLBAR))
        n.set_urgency(pynotify.URGENCY_LOW)
    else:
        n.set_icon_from_pixbuf(gtk.Label().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_LARGE_TOOLBAR))
        n.set_urgency(pynotify.URGENCY_LOW)
    #n.set_timeout(2)
    n.show()
Ici comme vous le voyez, je gére 4 type de notifications. Ce qui varie ici c'est l'icone utilisée, mais on peut aussi choisir le niveau d'urgence de la notification ainsi que le time out.
Le paramètre d'entrée de ma fonction est une string ressemblant à ça: titre de ma notification;texte de ma notification;2.
La variable "n" est ma notification, je la construit tout au long de la fonction et le n.show() permet d'afficher cette notification sur le bureau. Les notifications s'adaptent au thèmes des notifications de votre système.

1 commentaire:

  1. Borgata Hotel Casino & Spa to close at 5pm tomorrow
    The Borgata Hotel Casino 아산 출장마사지 & Spa is 김포 출장안마 temporarily closing at 여주 출장안마 5pm. Check the full schedule, including venue information and 춘천 출장샵 available 양주 출장마사지 seating charts

    RépondreSupprimer