Skip to main content
Skip to content

Using Python bindings for KDE Frameworks

Extend your application with KDE libraries.

KDE provides Python bindings for some KDE libraries. The current list of available bindings can be seen on Python support in KDE. Most notably:

Bindings installation

Like with Kirigami and PySide6/PyQt6, the bindings need to be built against the same version of Qt used for PySide6/PyQt6.

Because of this, currently the easiest way to use KDE bindings is by using a distribution that has built KDE libraries with support for the Python bindings. As the bindings are still new, not all distributions will have bindings, so we recommend you to use distrobox. Use either Fedora Rawhide, or Arch Linux.

logo of Linux operating system ManjaroManjarologo of Linux operating system Arch LinuxArch
sudo pacman -S kcoreaddons kguiaddons knotifications kunitconversion kwidgetsaddons kxmlgui kstatusnotifieritem
logo of Linux operating system FedoraFedora
sudo dnf install python3-kf6-kcoreaddons python3-kf6-kguiaddons python3-kf6-knotifications python3-kf6-kunitconversion python3-kf6-kwidgetsaddons python3-kf6-kxmlgui python3-kf6-kstatusnotifieritem

Reading C++ documentation for use in Python

Over the KDE API documentation website, when viewing a class, the top section of the page should list any includes as well as the project to which it belongs:

KDE API Reference / The KDE Frameworks / KCoreAddons
#include <KAboutData>

In Python one would translate this to the following import:

from KCoreAddons import KAboutData

Where in C++ you'd use:

KAboutData aboutData(
    "foo", "Foo", "0.1",
    "To Foo or not To Foo",
    KAboutLicense::GPL,
    "Copyright 2017 Bar Foundation"), QString(),
    "https://www.foo-the-app.net");
aboutData.setDesktopFileName("org.barfoundation.foo"); 
KAboutData::setApplicationData(aboutData);

auto kdeIcon = QIcon::fromTheme(QStringLiteral("kde")
QApplication::setWindowIcon(kdeIcon);

In Python you'd write:

aboutData = KAboutData(
    "foo", "Foo", "0.1",
    "To Foo or not To Foo",
    KAboutLicense.GPL,
    "Copyright 2017 Bar Foundation", "",
    "https://www.foo-the-app.net");
aboutData.setDesktopFileName("org.barfoundation.foo");
KAboutData.setApplicationData(aboutData);
 
kdeIcon = QIcon.fromTheme("kde")
QApplication.setWindowIcon(kdeIcon);

PySide6 has no QString or QStringLiteral, instead opting for the native Python str type. As such, the default QString() constructor which creates an empty QString can be replaced with "", and any QStringLiteral() can just be disconsidered.

Static functions which have :: (like QIcon::fromTheme()) can be called instead with . in Python. The same applies to enums (like KAboutLicense::GPL).