PyGObject
PyGObject is a Python package providing bindings for GObject-based libraries, including GTK, GStreamer, WebKitGTK, GLib, and GIO. It allows Python applications to interact with the entire GNOME software platform through GObject Introspection. The current version is 3.56.2 and it supports Linux, Windows, macOS, and Python 3.10+.
Warnings
- breaking The `pygtkcompat` module, used for porting legacy GTK2/Python2 code, was deprecated in PyGObject 3.48 and entirely removed in 3.52.0. Code relying on this compatibility layer will break.
- breaking PyGObject 3.55.0 removed 'toggle references'. If you used Python weakrefs to track PyGObjects, you must change them to `GObject.Object.weak_ref()`.
- gotcha Installing PyGObject via pip often requires system-level development packages (e.g., GTK development files, gobject-introspection, a C compiler) which are not installed by `pip`. The `pip install` command is primarily for the Python bindings, not the underlying GObject/GTK libraries.
- gotcha When importing `Gdk` or `Gtk`, their `init_check()` functions are automatically called for backwards compatibility. This prevents calling `Gtk.disable_setlocale()` as it must be called before GTK initialization.
- deprecated Calling GLib API through the GObject namespace (e.g., `GObject.some_glib_function()`) was deprecated in PyGObject 3.7.2. This usage will likely be removed in future versions.
Install
-
pip install PyGObject
Imports
- gi
import gi
- Gtk
from gi.repository import Gtk
- require_version
gi.require_version('Gtk', '4.0')
Quickstart
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import GLib, Gtk
class MyApplication(Gtk.Application):
def __init__(self):
super().__init__(application_id="com.example.MyGtkApplication")
GLib.set_application_name('My Gtk Application')
def do_activate(self):
window = Gtk.ApplicationWindow(application=self, title="Hello World")
window.present()
app = MyApplication()
app.run()