Gtk+ and Python

Overview

Most of the Gtk stack provides bindings via gobject-introspection that is built as part of a normal gtk-osx build. To use those bindings with Python you need pygobject, pycairo, and perhaps a python build of gtk-mac-integration. Those modules are provided by meta-gtk-osx-python3-gtk3 and the first two by meta-gtk-osx-python3-gtk4.

Note that Python2 and Gtk2 are obsolete and no longer supported by Gtk-OSX.

Building

Include in your modules, either as a dependency of your application build or on the command line *with all of the other modules* either meta-gtk-osx-python3-gtk3 or meta-gtk-osx-python3-gtk4 depending on which version of Gtk your Python application uses, then build as usual. If you're planning to bundle your application you'll also need python3 in your modules list. Note that for libxml2 to build the correct set of python bindings you need to build python3}} before {{{meta-gtk-osx-bootstrap. As long as they're both on the modules list when you build jhbuild will take care of getting everything in the right order. If you don't build python3 jhbuild will link the pygobject loadable modules with the libpython.dylib in the jhbuild virtualenv, rendering your build unsuitable for bundling.

Bundling

Please read Bundling for the details of creating an application bundle.

To avoid needing the overhead of the full interpreter gtk-mac-bundler includes in the examples directory a C program that can link to libpython3.dylib and set up the environment for most Python3 Gtk programs. You may need to customize it for the needs of your application.

See the https://gitlab.gnome.org/GNOME/gtk-mac-bundler/-/blob/master/examples/python-launcher.bundle python-launcher.bundle bundle-file for an example of how to set up a python3 application bundle. Note in particular the compile instructions for python-launcher.c near the top of the file. XML comments don't allow -- in comments so the options are written e.g. -\-cflags; change that to -- when you invoke the compile command.

Note as well the https://gitlab.gnome.org/GNOME/gtk-mac-bundler/-/blob/master/examples/python-entitlements.plist entitlements file. The one entitlement in it, allow-unsigned-executable-memory, is needed for macOS to allow byte-compiled python to execute. You can alternatively pre-byte-compile all of your python files into .pyc or .pyo files, sign them, and include them in the bundle instead of the plain text files but you'll have to write your own script to do so.

Your application may require other entitlements, just add them to a customized version of the python-entitlements.plist and point the entitlements link in the bundle file at it.

Projects/GTK/OSX/Python (last edited 2022-06-03 19:53:52 by jralls)