Skip to content

Instantly share code, notes, and snippets.

@codito
Last active April 5, 2021 13:07
Show Gist options
  • Save codito/dbd64bdc8cd51c58741a59537874e0be to your computer and use it in GitHub Desktop.
Save codito/dbd64bdc8cd51c58741a59537874e0be to your computer and use it in GitHub Desktop.
QtDBus demo with PyQt5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore, QtDBus, QtWidgets
def notify(header, msg):
item = "org.freedesktop.Notifications"
path = "/org/freedesktop/Notifications"
interface = "org.freedesktop.Notifications"
app_name = "dbus_demo"
v = QtCore.QVariant(12321) # random int to identify all notifications
if v.convert(QtCore.QVariant.UInt):
id_replace = v
icon = ""
title = header
text = msg
actions_list = QtDBus.QDBusArgument([], QtCore.QMetaType.QStringList)
hint = []
time = 100 # milliseconds for display timeout
bus = QtDBus.QDBusConnection.sessionBus()
if not bus.isConnected():
print("Not connected to dbus!")
notify = QtDBus.QDBusInterface(item, path, interface, bus)
if notify.isValid():
x = notify.call(QtDBus.QDBus.AutoDetect, "Notify", app_name,
id_replace, icon, title, text,
actions_list, hint, time)
if x.errorName():
print("Failed to send notification!")
print(x.errorMessage())
else:
print("Invalid dbus interface")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
notify("hello header", "some message!")
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment