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-notify2或python3-notify2并通过它调用通知:
import notify2
def sendmessage(title, message):
notify2.init("Test")
notice = notify2.Notification(title, message)
notice.show()
return
Run Code Online (Sandbox Code Playgroud)
fos*_*dom 12
虽然您可以调用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)
要回答 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)
小智 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)
| 归档时间: |
|
| 查看次数: |
43083 次 |
| 最近记录: |