如何向通知氣泡發送簡訊?

如何向通知氣泡發送簡訊?

我編寫了一個 python 程式碼,用於將隨機文字放入 .txt 檔案中。現在我想透過「notify-send」命令將此隨機文字傳送到通知區域。我們該怎麼做呢?

答案1

我們隨時可以打電話通知發送作為子進程,例如:

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

import subprocess

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

或者我們也可以安裝python-notify2或者python3-notify2並透過以下方式呼叫通知:

import notify2

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

答案2

蟒蛇3

您可以notify-send通過os.systemsubprocess可以說使用 Notify 與基於 GTK3 的程式設計更加一致gobject-內省班級。

一個小例子將展示這一點:

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)

相關內容