Create a gist now

Instantly share code, notes, and snippets.

@codito /dbus_demo.py
Last active Sep 24, 2017

What would you like to do?
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