
ランダムなテキストを .txt ファイルに取り込むための Python コードを書きました。次に、このランダムなテキストを 'notify-send' コマンドで通知領域に送信したいと思います。どうすればいいでしょうか?
答え1
いつでもお電話ください通知送信サブプロセスとして、例えば次のようになります:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import subprocess
def sendmessage(message):
subprocess.Popen(['notify-send', message])
return
あるいはインストールすることもできますPython 通知 2またはpython3-通知2そしてそれを通じて通知を呼び出します:
import notify2
def sendmessage(title, message):
notify2.init("Test")
notice = notify2.Notification(title, message)
notice.show()
return
答え2
パイソン3
notify-send
または経由で呼び出すこともできますos.system
が、subprocess
おそらくNotifyを使用する方がGTK3ベースのプログラミングと一貫性があります。gオブジェクトイントロスペクションクラス。
小さな例でこれを実際に示します:
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")
答え3
Mehul Mohan の質問に答えるとともに、タイトルとメッセージのセクションを含む通知をプッシュする最も短い方法を提案します。
import os
os.system('notify-send "TITLE" "MESSAGE"')
これを関数内に置くと、引用符の中に引用符があるため少し混乱するかもしれません。
import os
def message(title, message):
os.system('notify-send "'+title+'" "'+message+'"')
答え4
import os
mstr='Hello'
os.system('notify-send '+mstr)