python - 为什么 Gio.Notification 没有在 Python 中显示?
问题描述
我想使用我的 Gtk 应用程序显示通知,但是当我在下面运行我的代码时,我一切正常,但通知没有显示,即使我单击按钮也是如此。我尝试使用此答案中建议的桌面文件来运行它,但它仍然无法正常工作。这是我的代码:
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
解决方案
我发现只需将优先级设置为高就会出现通知。请注意,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)
如果您指定字符串 id 而不是None
在发送通知时 ( self.send_notification("my_notif_id", notification)
),您可以稍后使用 撤回通知self.withdraw_notification("my_notif_id")
。
推荐阅读
- reactjs - 网络连接检查反应
- dialogflow-es - Dialogflow Intent 在从 Google Assistant 的列表视图键中单击时不匹配,但在以纯文本形式编写时有效
- amazon-web-services - API Gateway HTTP 客户端请求与 IAM 身份验证与 Go
- ios - 无法使用 CloudKit 获取 sharedCloudDatabase 自定义区域中的记录
- c# - unity 无法使用 kinect V2 录制音频
- node.js - 如何在节点js中从服务返回两个以上的值到控制器
- reporting-services - 如何设置月初到昨天日期的默认参数日期
- machine-learning - Firebase MLKit 文本识别在特定情况下无法识别数字
- android - 我的 Android 应用程序被 PlayStore 拒绝
- python - 如何在列表中找到最小正数