【问题标题】:Why is Gio.Notification not showing in Python?为什么 Gio.Notification 没有在 Python 中显示?
【发布时间】:2021-09-17 00:13:50
【问题描述】:

我想使用我的 Gtk 应用程序显示通知,但是当我在下面运行我的代码时,我一切正常,但通知没有显示,即使我单击按钮也是如此。我尝试使用this answer 中建议的桌面文件来运行它,但它仍然无法正常工作。这是我的代码:

import gi
import sys

gi.require_version("Gtk", "3.0")
from gi.repository import Gio, Gtk

class App(Gtk.Application):
    
    def __init__(self, *args, **kwargs):
        Gtk.Application.__init__(self, *args, application_id="org.example.myapp", **kwargs)
        self.window = None

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        if not self.window:
            self.button = Gtk.Button(label="send notification")
            self.button.connect("clicked", self.notnotnot)
            self.window = Gtk.ApplicationWindow(application=self)
            self.window.add(self.button)
            self.window.show_all()
            self.window.present()

    def notnotnot(self, *args):
        notification = Gio.Notification()
        notification.set_body("Hello!")
        self.send_notification(None, notification)

if __name__ == "__main__":
    app = App()
    app.run(sys.argv)

这是桌面文件org.example.myapp.desktop

[Desktop Entry]
Type=Application
Name=My Application
Exec=python3 /home/user/programs/python/testing/SO/problem_why_is_gtk....py
Terminal=true
X-GNOME-UsesNotifications=true

【问题讨论】:

  • xfce4-notifyd 下为我工作。唯一的问题是标题显示为 '[Invalid UTF-8]' 并且我在控制台中收到警告 'g_variant_new_string: assertion 'string != NULL' failed',但添加对 notification.set_title 的调用可以解决此问题。

标签: python notifications gtk


【解决方案1】:

我发现只要将优先级设置为高就会显示通知。请注意,Gio.Notification.set_urgent() 已弃用。您需要使用Gio.Notification.set_priority()。以下是相应标记添加行的代码:

import gi
import sys

gi.require_version("Gtk", "3.0")
from gi.repository import Gio, Gtk

class App(Gtk.Application):
    
    def __init__(self, *args, **kwargs):
        Gtk.Application.__init__(self, *args, application_id="org.example.myapp", **kwargs)
        self.window = None

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        if not self.window:
            self.button = Gtk.Button(label="send notification")
            self.button.connect("clicked", self.notnotnot)
            self.window = Gtk.ApplicationWindow(application=self)
            self.window.add(self.button)
            self.window.show_all()
            self.window.present()

    def notnotnot(self, *args):
        notification = Gio.Notification()
        notification.set_body("Hello!")
        notification.set_priority(Gio.NotificationPriority.HIGH) ### ADDED LINE
        self.send_notification(None, notification)

if __name__ == "__main__":
    app = App()
    app.run(sys.argv)

如果您在发送通知 (self.send_notification("my_notif_id", notification)) 时指定字符串 id 而不是None,您可以稍后使用self.withdraw_notification("my_notif_id") 撤回通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-16
    • 2014-11-30
    • 2020-06-13
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    相关资源
    最近更新 更多