Como envio mensagens de texto para os balões de notificação?

Como envio mensagens de texto para os balões de notificação?

Escrevi um código python para obter texto aleatório em um arquivo .txt. Agora quero enviar este texto aleatório para a área de notificação por meio do comando 'notify-send'. Como fazemos isso?

Responder1

Sempre podemos ligarnotificar-enviarcomo um subprocesso, por exemplo, assim:

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

import subprocess

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

Alternativamente, também poderíamos instalarpython-notify2oupython3-notify2e chame a notificação através disso:

import notify2

def sendmessage(title, message):
    notify2.init("Test")
    notice = notify2.Notification(title, message)
    notice.show()
    return

Responder2

python3

Embora você possa ligar notify-sendvia os.systemou subprocessé indiscutivelmente mais consistente com a programação baseada em GTK3 usar o Notifyintrospecção de objetoaula.

Um pequeno exemplo mostrará isso em ação:

from gi.repository import GObject
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, file_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")

Responder3

Para responder à pergunta de Mehul Mohan, bem como propor o caminho mais curto para enviar uma notificação com seções de título e mensagem:

import os
os.system('notify-send "TITLE" "MESSAGE"')

Colocar isso em função pode ser um pouco confuso devido às aspas entre aspas

import os
def message(title, message):
  os.system('notify-send "'+title+'" "'+message+'"')

Responder4

import os
mstr='Hello'
os.system('notify-send '+mstr)

informação relacionada