Using Python bindings for KDE Frameworks
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.
Currently only one distribution provides these bindings:
distrobox create --image archlinux --name arch
distrobox enter arch
sudo pacman -Syu
sudo pacman -S pyside6 kcoreaddons # Or any other required library
Contributing to the bindings
Click here to read how to contribute or request for bindings
If you want a specific KDE library to be available as a Python binding, mention your interest in Python support in KDE or on the KDE Python Matrix group. If your distribution doesn't yet provide Python bindings for a KDE library that can already export them, you may request your distribution to do so. Lastly, feel free to Send a merge request and make more libraries available as Python bindings!
To generate the KDE Frameworks bindings, their respective upstream libraries in C++ need to use ECMGeneratePythonBindings. When each project is built, it generates a CPython .so
file that is typically installed under /usr/lib64/python3.11/site-packages/
by Linux distributions. This makes it available to be used in Python scripts. If the site-packages
files are installed elsewhere, you can force-use them in Python with PYTHONPATH
.
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
).