Skip to content

Instantly share code, notes, and snippets.

@meramsey
Forked from codito/dbus_demo.py
Created April 5, 2021 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meramsey/6ade250bbe09998dee4c37a5c604b969 to your computer and use it in GitHub Desktop.
Save meramsey/6ade250bbe09998dee4c37a5c604b969 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