如何向通知气泡发送短信?

Anu*_*pam 49 python notify-osd

我编写了一个 python 代码,用于将随机文本放入 .txt 文件中。现在我想通过“notify-send”命令将此随机文本发送到通知区域。我们怎么做?

Tak*_*kat 74

我们总是可以将通知发送作为子进程调用,例如:

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

import subprocess

def sendmessage(message):
    subprocess.Popen(['notify-send', message])
    return
Run Code Online (Sandbox Code Playgroud)

或者,我们也可以安装python-notify2python3-notify2并通过它调用通知:

import notify2

def sendmessage(title, message):
    notify2.init("Test")
    notice = notify2.Notification(title, message)
    notice.show()
    return
Run Code Online (Sandbox Code Playgroud)

  • 我不得不使用 `subprocess.Popen(['notify-send', message])` 来使第一个示例工作。 (2认同)

fos*_*dom 12

蟒蛇3

虽然您可以调用notify-sendviaos.system或者subprocess使用 Notify gobject-introspection类可以说更符合基于 GTK3 的编程。

一个小例子将展示这一点:

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")
Run Code Online (Sandbox Code Playgroud)


Sil*_*vee 7

要回答 Mehul Mohan 问题并提出推送带有标题和消息部分的通知的最短方法:

import os
os.system('notify-send "TITLE" "MESSAGE"')
Run Code Online (Sandbox Code Playgroud)

由于引号中的引号,将其放入函数中可能会有点混乱

import os
def message(title, message):
  os.system('notify-send "'+title+'" "'+message+'"')
Run Code Online (Sandbox Code Playgroud)

  • 您可以建议 [对该帖子进行编辑。](http://askubuntu.com/posts/479244/edit) (4认同)
  • 使用 `'notify-send "{}" "{}"'.format(title, message)` 而不是添加字符串怎么样? (3认同)

小智 7

你应该使用notify2包,它是python-notify的替代品。如下使用。

pip install notify2
Run Code Online (Sandbox Code Playgroud)

和代码:

import notify2
notify2.init('app name')
n = notify2.Notification('title', 'message')
n.show()
Run Code Online (Sandbox Code Playgroud)


小智 6

import os
mstr='Hello'
os.system('notify-send '+mstr)
Run Code Online (Sandbox Code Playgroud)

  • 此版本容易受到任意命令执行的影响。 (3认同)

小智 5

对于在 +2018 中查看此内容的任何人,我可以推荐notify2包。

这是notify-python 的纯python 替代品,使用python-dbus 直接与通知服务器通信。它与 Python 2 和 3 兼容,并且它的回调可以与 Gtk 3 或 Qt 4 应用程序一起使用。