알림 풍선으로 문자 메시지를 보내려면 어떻게 해야 하나요?

알림 풍선으로 문자 메시지를 보내려면 어떻게 해야 하나요?

나는 임의의 텍스트를 .txt 파일로 가져오기 위한 Python 코드를 작성했습니다. 이제 'notify-send' 명령을 통해 이 임의의 텍스트를 알림 영역으로 보내고 싶습니다. 어떻게 해야 할까요?

답변1

우리는 언제나 전화할 수 있어요통지-전송하위 프로세스로, 예를 들어 다음과 같습니다:

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

import subprocess

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

대안으로 우리는 또한 설치할 수 있습니다파이썬-notify2또는python3-notify2다음을 통해 알림을 호출합니다.

import notify2

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

답변2

파이썬3

Notify를 사용하는 것은 GTK3 기반 프로그래밍을 notify-send통해 호출할 수 있지만 틀림 os.system없이 더 일관성이 있습니다.subprocessgobject-introspection수업.

작은 예에서는 이를 실제로 보여줍니다.

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)

관련 정보