Last modified: Jun 01, 2025 By Alexander Williams
Install PyGObject for GTK+ in Python
PyGObject is a Python module for GTK+ development. It allows you to create GUI applications easily. This guide will help you install it on any OS.
What is PyGObject?
PyGObject provides Python bindings for GObject-based libraries. These include GTK, GStreamer, and WebKitGTK. It replaces older bindings like PyGTK.
With PyGObject, you can build modern GUI apps in Python. It works on Windows, Linux, and macOS.
Prerequisites
Before installing PyGObject, ensure you have:
- Python 3.6 or later
- pip package manager
- GTK+ 3.0 development files
Check your Python version with python --version
. For pip, use pip --version
.
Install PyGObject on Linux
Linux users can install PyGObject via package managers. The commands differ by distribution.
Ubuntu/Debian
sudo apt-get install python3-gi python3-gi-cairo gir1.2-gtk-3.0
Fedora
sudo dnf install pygobject3 python3-gobject
Install PyGObject on Windows
Windows users can install PyGObject using pip. First, install GTK+ runtime from their official site.
pip install PyGObject
Ensure you add GTK+ to your system PATH. This lets Python find the required libraries.
Install PyGObject on macOS
macOS users can install PyGObject via Homebrew. First, install GTK+ 3.
brew install gtk+3 pygobject3
Verify Installation
After installing, verify it works. Create a test script:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window(title="Hello World")
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Run the script. A small window should appear. This confirms PyGObject works.
Troubleshooting
If you face issues, check these common solutions:
ImportError: Ensure GTK+ is installed correctly. The error often means missing dependencies.
Version conflicts: Use gi.require_version()
to specify the GTK+ version.
For complex projects, consider using virtual environments to manage dependencies.
PyGObject Basics
Here's a simple example to create a button:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def on_button_clicked(button):
print("Hello World")
win = Gtk.Window(title="Button Example")
button = Gtk.Button(label="Click Me")
button.connect("clicked", on_button_clicked)
win.add(button)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
This creates a window with a clickable button. The button prints a message when clicked.
Advanced Usage
PyGObject supports advanced GTK+ features. You can build complex interfaces with:
- Grid layouts
- Tree views
- Custom widgets
For data-heavy apps, combine PyGObject with PyTables for efficient data handling.
Conclusion
PyGObject makes GTK+ development easy in Python. It works across platforms and supports modern GTK+ features.
Follow this guide to install it on your system. Start building GUI apps today. For machine learning UIs, check SHAP for explainable AI features.