Because the org.freedesktop.PowerManagement is on the Session Bus, we create an object self.sessionBus that represents that bus. We also grab our powerdevil Object via its path /moddules/powerdevil. We can now call methods on the object, but first we need to set up the gui framework, a button and its connection (via Qt signals/slots) to a python method that will call the dbus method.

Because the org.freedesktop.PowerManagement is on the Session Bus, we create an object self.sessionBus that represents that bus. We also grab our powerdevil Object via its path /moddules/powerdevil. We can now call methods on the object, but first we need to set up the gui framework, a button and its connection (via Qt signals/slots) to a python method that will call the dbus method.

Line 180:

Line 180:

==== Layout and Buttons ====

==== Layout and Buttons ====

−

<code python>

+

<syntaxhighlight lang="python">

self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)

self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)

self.setLayout(self.layout)

self.setLayout(self.layout)

Line 190:

Line 190:

self.layout.addWidget(button)

self.layout.addWidget(button)

QObject.connect(button, SIGNAL("clicked()"), self.screenOff)

QObject.connect(button, SIGNAL("clicked()"), self.screenOff)

−

</code>

+

</syntaxhighlight>

If you need help understanding the above, you should consult the reading list linked at the top of this page.

If you need help understanding the above, you should consult the reading list linked at the top of this page.

Line 198:

Line 198:

We now need to define the self.screenOff method that we have just connected to the button.

We now need to define the self.screenOff method that we have just connected to the button.

−

<code python>

+

<syntaxhighlight lang="python">

def screenOff(self):

def screenOff(self):

self.powerdevil.turnOffScreen(dbus_interface='org.kde.PowerDevil')

self.powerdevil.turnOffScreen(dbus_interface='org.kde.PowerDevil')

−

</code>

+

</syntaxhighlight>

It's relatively simple. We call the method turnOffScreen() on our powerdevil object (the method turnOffScreen was shown on the interface org.kde.PowerDevil in qdbusviewer)

It's relatively simple. We call the method turnOffScreen() on our powerdevil object (the method turnOffScreen was shown on the interface org.kde.PowerDevil in qdbusviewer)

The aim of this tutorial is to give an overview of DBus in the context of python. By the end, the goal is to understand how to find DBus methods in existing applications and how to use them in PyQt4/PyKDE4. It assumes a basic working knowledge of Python and PyKDE4.

First off, a small introduction to DBus. DBus is an inter-process communication framework. In other words, it allows different applications to talk to each other. DBus uses the concept of signals and methods, which are similar to the Signals and Slots in Qt. (For more information on Signals and Slots in python, suggested reading at the top) You can connect signals to methods, or call methods directly.

Everything sent or received in dbus is transferred over a bus. There are two main buses available, the Session Bus and the System Bus. The former handles per-session (per-user) information while the System Bus handles systemwide notification and settings. For example, the hal subsystem provides a number of interfaces on the System Bus, while Amarok or PowerDevil provides session-specific interfaces for control over music and power options, respectively.

A DBus path is made up of three parts, the Service Name, the Object Path and the Interface. The following table (taken from the Qt intro to dbus) provides some ways to distinguish between the three parts.

Lets go through this example to try and understand it. First we import the libraries that are needed to make it all work. Most of the Qt and KDE ones should be familiar to you, but to get dbus we need the one at the end: "import dbus".

Because the org.freedesktop.PowerManagement is on the Session Bus, we create an object self.sessionBus that represents that bus. We also grab our powerdevil Object via its path /moddules/powerdevil. We can now call methods on the object, but first we need to set up the gui framework, a button and its connection (via Qt signals/slots) to a python method that will call the dbus method.