{"id":5396,"library":"pygobject","title":"PyGObject","description":"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+.","status":"active","version":"3.56.2","language":"en","source_language":"en","source_url":"https://gitlab.gnome.org/GNOME/pygobject","tags":["GUI","Gtk","Gnome","GObject","bindings","desktop-app"],"install":[{"cmd":"pip install PyGObject","lang":"bash","label":"PyPI (requires system dependencies)"}],"dependencies":[{"reason":"Commonly used for drawing operations within GTK applications, often installed alongside PyGObject.","package":"pycairo","optional":true}],"imports":[{"note":"Old static bindings (e.g., 'import gtk' for GTK2) are deprecated and no longer supported in modern PyGObject versions, which use GObject Introspection via 'gi.repository'.","wrong":"import gtk # or import glib","symbol":"gi","correct":"import gi"},{"note":"Accessed through the 'gi.repository' namespace after calling 'gi.require_version()'.","symbol":"Gtk","correct":"from gi.repository import Gtk"},{"note":"It is crucial to call gi.require_version() before importing from gi.repository to specify the desired version of a library (e.g., GTK 4.0), preventing conflicts or unexpected behavior if multiple versions are installed on the system.","wrong":"from gi.repository import Gtk # directly","symbol":"require_version","correct":"gi.require_version('Gtk', '4.0')"}],"quickstart":{"code":"import gi\ngi.require_version('Gtk', '4.0')\nfrom gi.repository import GLib, Gtk\n\nclass MyApplication(Gtk.Application):\n    def __init__(self):\n        super().__init__(application_id=\"com.example.MyGtkApplication\")\n        GLib.set_application_name('My Gtk Application')\n\n    def do_activate(self):\n        window = Gtk.ApplicationWindow(application=self, title=\"Hello World\")\n        window.present()\n\napp = MyApplication()\napp.run()","lang":"python","description":"This 'Hello World' example demonstrates a basic GTK4 application using PyGObject. It initializes the GTK4 library, creates a Gtk.Application, and displays a window with 'Hello World' as its title."},"warnings":[{"fix":"Migrate code fully to modern PyGObject and GTK3+ APIs. Refer to PyGObject's 'Porting from Static Bindings' documentation.","message":"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.","severity":"breaking","affected_versions":">=3.52.0"},{"fix":"Replace Python weakrefs with `GObject.Object.weak_ref()` for tracking GObjects.","message":"PyGObject 3.55.0 removed 'toggle references'. If you used Python weakrefs to track PyGObjects, you must change them to `GObject.Object.weak_ref()`.","severity":"breaking","affected_versions":">=3.55.0"},{"fix":"Follow platform-specific installation instructions for system dependencies (e.g., `sudo apt install python3-gi gir1.2-gtk-4.0` on Debian/Ubuntu, MSYS2 for Windows) before `pip install PyGObject`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To skip auto-initialization, call `gi.disable_legacy_autoinit()` before importing `Gdk` or `Gtk`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Call GLib API directly through the `GLib` namespace (e.g., `GLib.some_function()`) after importing `GLib` from `gi.repository`.","message":"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.","severity":"deprecated","affected_versions":">=3.7.2"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}